我有一個定義的路徑在XAML:如何在運行時將已在XAML ResourceDictionary中定義的路徑多次添加到WPF表單中?
<UserControl.Resources>
<ResourceDictionary>
<Path x:Key="N44" Width="20" Height="80" Stretch="Fill" Fill="#FF000000" Data="M 20,25.2941L 20,29.4118L 15.9091,29.4118L 15.9091,40L 12.2727,40L 12.2727,29.4118L 2.54313e-006,29.4118L 2.54313e-006,25.6985L 13.4872,7.62939e-006L 15.9091,7.62939e-006L 15.9091,25.2941L 20,25.2941 Z M 12.2727,25.2941L 12.2727,5.28493L 2.09517,25.2941L 12.2727,25.2941 Z M 20,65.2941L 20,69.4118L 15.9091,69.4118L 15.9091,80L 12.2727,80L 12.2727,69.4118L -5.08626e-006,69.4118L -5.08626e-006,65.6985L 13.4872,40L 15.9091,40L 15.9091,65.2941L 20,65.2941 Z M 12.2727,65.2941L 12.2727,45.2849L 2.09517,65.2941L 12.2727,65.2941 Z "/>
</ResourceDictionary>
</UserControl.Resources>
我想將它添加到WPF網格&做一次這樣的工作:
System.Windows.Shapes.Path aPath = new System.Windows.Shapes.Path();
aPath = (System.Windows.Shapes.Path)this.Resources["N44"];
LayoutRoot.Children.Add(aPath);
但是,如果我上的按鈕添加以下代碼單擊事件,然後單擊該按鈕兩次,引發錯誤說明
「指定的Visual已經是另一個Visual或Roo的子項 t的 CompositionTarget。「
然後我試圖創建資源的兩個實例,但我一直收到相同的錯誤。下面是我本次測試使用的代碼:
private void cmbTest_Click(object sender, System.Windows.RoutedEventArgs e)
{
System.Windows.Shapes.Path aPath = new System.Windows.Shapes.Path();
aPath = (System.Windows.Shapes.Path)this.Resources["N44"];
if (LayoutRoot.Children.Contains(aPath) == true){
System.Windows.Shapes.Path bPath = (System.Windows.Shapes.Path)this.Resources["N44"];
LayoutRoot.Children.Add(bPath);
}else{
aPath.Name = "a";
aPath.Tag = "a";
LayoutRoot.Children.Add(aPath);
}
}
這樣,我怎麼能在運行時添加一個XAML路徑,它已在ResourceDictionary中被定義,多次到WPF的形式?
你知道如何在XAML代碼中引用'N44'的數據嗎? – 2015-04-20 05:01:50
這個問題是> 5歲。也許你應該問一個新問題。 – 2015-04-20 08:29:46