1

我收到以下错误消息:“在附加到新的父视觉之前,必须断开指定子与当前父视觉的连接”

我知道问题出在哪里,但我无法在我的实际应用程序中修复它,我使用BeginningEdit()事件弹出一个新窗口来编辑包含麻烦制造者的 myObjectFlowdocument

我也使用我的克隆扩展来创建一个new Flowdocument,希望能否认这个错误,但它不会改变任何东西

XAML

<DataGrid AutoGenerateColumns="False" Name="myDGrid">        
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="myHeader">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <FlowDocumentScrollViewer Width="100" Height="50"                                
                                                      HorizontalScrollBarVisibility="Hidden" 
                                                      VerticalScrollBarVisibility="Hidden" 
                                                      FontStretch="Normal" FontSize="13" FontWeight="Bold" 
                                                      HorizontalContentAlignment="Center" 
                                                      VerticalContentAlignment="Center"  
                                                      IsManipulationEnabled="False" IsSelectionEnabled="False"
                                                     Document="{Binding}">                                
                            </FlowDocumentScrollViewer>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>

                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <Label/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>

                </DataGridTemplateColumn>
            </DataGrid.Columns>               
        </DataGrid>

代码隐藏

    public string myFlowString
    {
        get
        {
            return (@"<FlowDocument PagePadding='0,0,0,0' Background='Green' AllowDrop='True' xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>
                            <Paragraph Background='Red' TextAlignment='Center'> some text befor <LineBreak/> Komponente 1<Run FontFamily='Palatino Linotype' Typography.Variants='Superscript'>1,2,3,4</Run>
                                <LineBreak/> Nachtisch</Paragraph> </FlowDocument>");
        }
    }

    public MainWindow()
    {
        InitializeComponent();

         var _mylist = new List<FlowDocument>();

        var myFlowDoc = XamlReader.Load(XmlReader.Create(new StringReader(myFlowString))) as FlowDocument;



        for (int i = 0; i < 10;i++ )
            _mylist.Add(myFlowDoc.Clone());

        myDGrid.ItemsSource = _mylist;
    }

}

public static class Extension
{
    public static FlowDocument Clone(this FlowDocument flowdoc)
    {
        var xml = XamlWriter.Save(flowdoc);
        return xml.toFlowDocument();
    }

    public static FlowDocument toFlowDocument(this string xamlString)
    {
        var stringReader = new StringReader(xamlString);
        var xmlReader = System.Xml.XmlReader.Create(stringReader);

        return XamlReader.Load(xmlReader) as FlowDocument;
    }
}
4

1 回答 1

1

可以解决这个问题,你应该使用 2 个属性,每个属性DataGridCell
为你的CellTemplate
一个为你的CellEditingTemplate

每次调用您的 getter 时,您都必须创建一个新的FlowDocument,因为它看起来像是在DataGridCell保存此类数据的方式上存在错误/故障。

于 2013-03-26T10:42:54.800 回答