我需要在FlowDocument中临时显示一些UIElement。我将其封装在BlockUIContainer中,但是当不再需要它时,我看不到如何将其与BlockUICOntainer断开连接。没有删除方法。下面的代码显示-它以异常结尾
System.InvalidOperationException:'指定的元素已经是另一个元素的逻辑子级。首先断开连接。'
例如
<Grid x:Name="Grid1">
<RichTextBox x:Name="rtb"/>
</Grid>
public MainWindow() {
InitializeComponent();
Loaded += MainWindow_Loaded;
}
private void MainWindow_Loaded(object sender,RoutedEventArgs e) {
var c2 = new WrapPanel();
// Imagine adding lots of controls into c2 ...
rtb.Document.Blocks.Add(new BlockUIContainer(c2));
var bc = (BlockUIContainer)c2.Parent;
((FlowDocument)(bc).Parent).Blocks.Remove(bc);
Grid1.Children.Add(c2); // Exception. Here I want to move that c2 elsewhere in logical tree but I don't know how to disconnect it
}
好的,我可以重新创建c2,但这不是很好。或者我看到我可以调用内部RemoveLogicalChild,但这似乎也很hacky。 WPF期望这样做如何?
谢谢