that's resourcedictionay file: TopologyTree.xaml
<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>
Side C#
objectTree.Resources = new ResourceDictionary();
objectTree.Resources.Source = new Uri("GUI/TopologyTree.xaml", UriKind.Relative);
while objectTree is a TreeView
how ever that doesn't work.
I have tried as followed which worked, but I need to redefine DataType here, so I think it's not so good.
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
);
What's more, I have tried put the content of xaml into the xmal window directly as followed, That works, but I need it be loaded dyanmically, so it just proven that my xmal is good.
<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>
Could anyone help me have a way to use it in C# side simply?