我收到以下错误消息:“在附加到新的父视觉之前,必须断开指定子与当前父视觉的连接”
我知道问题出在哪里,但我无法在我的实际应用程序中修复它,我使用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;
}
}