我正在尝试将模型中保存的对象集合绑定到WPF中的树形视图。我所使用的XML基于WPF Treeview Databinding Hierarchal Data with mixed types,但是我没有成功。
在我的MainWindow.Xaml.cs文件中,我有以下内容:
我的树形视图当前的XAML如下所示:
<TreeView Name="ConfigurationFilter">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type models:MyModel}" ItemsSource="{Binding Path=Filters.FilterType1}">
<StackPanel Orientation="Horizontal">
<CheckBox></CheckBox>
<Label Content="{Binding Path=Name}"></Label>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
我有一个像这样的模型
public class MyModel
{
public Observable<MyFilter> FilterType1 {get;set;}
public Observable<MyFilter> FilterType2 {get;set;}
}
public class MyFilter
{
public string Name {get;set;}
public bool IsSelected {get;set;}
}
在我的MainWindow.Xaml.cs文件中,我有以下内容:
public partial class MainWindow : Window
{
public MyModel Filters { get; set; }
}
FilterType1属性中有331个项目。但是当我运行应用程序时,绑定从未发生过。我在TreeView中看不到任何项目。我做错了什么?
更新1
如建议所述,我已将我的主窗口添加为TreeView的数据上下文和绑定,但我仍然无法在树中看到任何项目。
<TreeView Name="ConfigurationFilter" ItemsSource="{Binding Filters}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type models:MyModel}" ItemsSource="{Binding Path=Filters.FilterType1}">
<StackPanel Orientation="Horizontal">
<CheckBox></CheckBox>
<Label Content="{Binding Path=Name}"></Label>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
以及我的MainWindow.cs文件
public MainWindow()
{
InitializeComponent();
ConfigurationFilter.DataContext = this;
}