I got the following Error Message: "Must disconnect specified child from current parent Visual before attaching to new parent Visual"

I know whats the problem but I'm unable to fix it in my real application, I use the BeginningEdit() Event to popup a new Window for editing myObject which contains the troublemaker Flowdocument

I also use my Clone extension to create a new Flowdocument in the hope to deni this error but it doesn't change anything

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>

code-behind

    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;
    }
}
有帮助吗?

解决方案

OK to solve this problem you should use 2 properties per DataGridCell
one for your CellTemplate and
one for your CellEditingTemplate

every time your getter gets called you must create a new FlowDocument because it seams like there is a bug/glitch on how the DataGridCell holds such data.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top