我想将模型持有的对象的集合绑定到WPF中的树视图。我的XML做到这一点是基于WPF Treeview Databinding Hierarchal Data with mixed types,但我没有任何运气。在WPF中绑定到树视图
我目前的XAML看起来是这样的treeview。
<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;
}
我这就给了一枪,但它仍然没有奏效。我已经更新了我的问题,以使用您的示例显示我的XAML和MainWindow.cs代码。没有任何内容呈现给用户界面。你认为这是我试图绑定到对象内的属性,如Filters.FilterType1的问题? –
你是如何填充滤镜的?它看起来不像它将填充它的代码。 –
是的,MyObject是从存储库中获取的。在将对象返回到MainWindow之前,存储库使用数据填充FilterType集合。这发生在窗口加载方法 –