那是资源字典文件:TopologyTree.xaml。
此外,我已尝试将xaml内容直接放入xmal窗口中,如下所示。 这有效,但我需要它动态加载,所以这只是证明我的xmal很好。
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:autoDraw.ViewModel.Topology"
>
<HierarchicalDataTemplate x:Key="TopologyTreeBase" DataType="{x:Type local:Base}" ItemsSource="{Binding children}">
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsChecked}"></CheckBox>
<TextBlock Text="{Binding name}"></TextBlock>
</StackPanel>
</HierarchicalDataTemplate>
</ResourceDictionary>
侧面 C#
objectTree.Resources = new ResourceDictionary();
objectTree.Resources.Source = new Uri("GUI/TopologyTree.xaml", UriKind.Relative);
当objectTree是TreeView时
然而那并不起作用。
我尝试了以下方法,它是可行的,但我需要重新定义 DataType,所以我认为这不太好。
var resourceDictionary = new ResourceDictionary();
resourceDictionary.Source = new Uri("GUI/TopologyTree.xaml", UriKind.Relative);
objectTree.Resources.Add(
new DataTemplateKey(typeof(ViewModel.Topology.Base)),
resourceDictionary["TopologyTreeBase"] as HierarchicalDataTemplate
);
此外,我已尝试将xaml内容直接放入xmal窗口中,如下所示。 这有效,但我需要它动态加载,所以这只是证明我的xmal很好。
<TreeView Name="objectTree" Grid.Column="4" Margin="3" Grid.Row="1" Grid.RowSpan="3">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:Topology.Base}" ItemsSource="{Binding children}">
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsChecked}"></CheckBox>
<TextBlock Text="{Binding name}"></TextBlock>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
有人能帮我找到一种简单的方法在C#中使用它吗?