[英]Binding to a treeview in WPF
I am trying to bind a collection of objects held by a Model to a treeview in WPF.我试图将模型持有的一组对象绑定到 WPF 中的树视图。 My XML to do this was based on WPF Treeview Databinding Hierarchal Data with mixed types but I am not having any luck.
我的 XML 是基于混合类型的 WPF Treeview Databinding Hierarchal Data,但我没有任何运气。
My current XAML looks like this for the treeview.对于树视图,我当前的 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>
I have a model that looks like this我有一个看起来像这样的模型
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;}
}
Within my MainWindow.Xaml.cs I have the following:在我的 MainWindow.Xaml.cs 中,我有以下内容:
public partial class MainWindow : Window
{
public MyModel Filters { get; set; }
}
The FilterType1 property has 331 items in it. FilterType1 属性中有 331 个项目。 Yet when I run the app, the binding never happens.
然而,当我运行应用程序时,绑定从未发生。 I do not see any items in my Treeview.
我在 Treeview 中没有看到任何项目。 What am I doing wrong?
我究竟做错了什么?
Update 1更新 1
I have added my main window as the data context for the treeview and the binding as suggested but i still do not have any items in the tree我已将主窗口添加为树视图的数据上下文和建议的绑定,但树中仍然没有任何项目
<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>
and my MainWindow.cs和我的 MainWindow.cs
public MainWindow()
{
InitializeComponent();
ConfigurationFilter.DataContext = this;
}
I was able to get this working by modifying my treeview to bind to a composite view model as demonstrated on CodeProject .我能够通过修改我的 treeview 来绑定到一个复合视图模型来实现这个工作,如CodeProject 所示。
<TreeView Grid.ColumnSpan="2" Grid.RowSpan="2">
<TreeViewItem Header="Routes" Name="RouteView" ItemsSource="{Binding Routes}">
<TreeViewItem.ItemTemplate>
<HierarchicalDataTemplate DataType="{x:Type viewModels:RouteViewModel}" ItemsSource="{Binding}">
<StackPanel Orientation="Horizontal">
<Label Content="{Binding Path=Name}" VerticalAlignment="Center"></Label>
</StackPanel>
</HierarchicalDataTemplate>
</TreeViewItem.ItemTemplate>
</TreeViewItem>
</TreeView>
Since my treeview will have multiple root nodes that are pre-determined, I had to bind the datasource to each root TreeViewItem
.由于我的 treeview 将有多个预先确定的根节点,因此我必须将数据源绑定到每个根
TreeViewItem
。 Then I placed my objects into a composite viewmodel.然后我将我的对象放入一个复合视图模型中。
Composite View Model复合视图模型
public class DomainViewModel
{
public ReadOnlyCollection<RouteViewModel> Routes { get; set; }
public ReadOnlyCollection<SkyViewModel> SkyLines { get; set; }
public DomainViewModel(List<Route> routes)
{
Routes = new ReadOnlyCollection<RouteViewModel>(
(from route in routes
select new RouteViewModel(route))
.ToList<RouteViewModel>());
}
Skylines = new ReadOnlyCollection<SkyViewModel>(
(from route in routes
select new SkyViewModel(route))
.ToList<SkyViewModel>());
}
}
ViewModel - Only 1 shown to help readability. ViewModel - 仅显示 1 个以提高可读性。
public class RouteViewModel : INotifyPropertyChanged
{
private Route _route; // My actual database model.
public string Name
{
get { return _route.RouteName; }
}
public RouteViewModel(Route route)
{
_route = route;
}
}
MainWindow主窗口
public MainWindow()
{
InitializeComponent();
this.DomainFilterTreeView.DataContext = new this.Domains;
}
TreeView
doesn't have an ItemsSource
bound to. TreeView
没有绑定到ItemsSource
。 Assuming the DataContext
of the TreeView
is your MainWindow
, then you can add the ItemsSource
binding.假设
TreeView
的DataContext
是您的MainWindow
,那么您可以添加ItemsSource
绑定。
<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.xaml.cs主窗口.xaml.cs
public partial class MainWindow : Window
{
public MainWindow()
{
ConfigurationFilter.DataContext = this;
}
public MyModel Filters { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.