【发布时间】:2013-02-14 23:07:44
【问题描述】:
这是资源字典文件: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>
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# 端简单地使用它?
【问题讨论】:
-
您说文件的名称是 TopologyTreeDisplay.xaml 但您正在尝试加载 TopologyTree.xaml - 没有 display 部分
-
抱歉,这是TopologyTree.xaml extact,我在帖子中出错了
标签: c# wpf xaml resourcedictionary