更新时间:2023-11-27 20:25:40
的问题是,一个的UIElement
只能属于以一次一个家长。为了增加您的物品进入根
画布,你需要首先从 Template_canvas1
画布删除它们。
The problem is, that a UIElement
can only belong to one parent at a time. In order to add your items into the root
canvas, you need to first remove them from the Template_canvas1
canvas.
请参阅下面的代码。我创建的数组 UI元素
复制,然后把它们添加到 Template_canvas1 删除C>根。
See the following code. I create an array of the UIElements
to copy, and then remove them from Template_canvas1
before adding them to root
.
private void add_template_Click(object sender, RoutedEventArgs e)
{
var childrenList = Template_canvas1.Children.Cast<UIElement>().ToArray();
root.Children.Clear();
foreach (var c in childrenList)
{
Template_canvas1.Children.Remove(c);
root.Children.Add(c);
}
}
还有一个选择,如果你不这样做要从中删除 Template_canvas1
的项目,你可以创建UI元素的深层复制。另请参见下面的,我不会从 Template_canvas1
删除的项目:
There is one more option, if you don't want to remove the items from Template_canvas1
, you can create a Deep Copy of the UIElements. See also the following where I do not remove the items from Template_canvas1
:
private void add_template_Click(object sender, RoutedEventArgs e)
{
root.Children.Clear();
foreach (UIElement child in Template_canvas1.Children)
{
var xaml = System.Windows.Markup.XamlWriter.Save(child);
var deepCopy = System.Windows.Markup.XamlReader.Parse(xaml) as UIElement;
root.Children.Add(deepCopy);
}
}