【发布时间】:2014-02-05 21:55:04
【问题描述】:
我正在尝试将模型持有的对象集合绑定到 WPF 中的树视图。我的 XML 是基于 WPF Treeview Databinding Hierarchal Data with mixed types,但我没有任何运气。
对于树视图,我当前的 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 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;
}
【问题讨论】:
标签: c# wpf xaml data-binding treeview