[英]TreeView binding issue in WPF
Consider the following data structure: 考虑以下数据结构:
List<Person> People;
class Person {
List<Car> Cars;
List<Hobby> Hobbies;
}
I want to bind a TreeView to this structure. 我想将TreeView绑定到此结构。 And it should look like this: 它看起来应该是这样的:
People
> Frank
> Cars
> BMW
> Ford
> Hobbies
> Tennis
> Golf
> Jane
> Cars
> Hobbies
How can this be achieved in XAML? 如何在XAML中实现这一目标? Here's what I've got so far: 这是我到目前为止所得到的:
<TreeView>
<TreeView.Resources>
<DataTemplate x:Key="PersonTemplate">
<TextBlock Header="{Binding Name}">
<TextBlock.ContextMenu>
<ContextMenu>
<MenuItem Header="Remove" />
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
</DataTemplate>
</TreeView.Resources>
<TreeViewItem Header="{Binding Name}"IsExpanded="True" >
<TreeViewItem Header="People"
ItemsSource="{Binding People}"
ItemTemplate="{StaticResource PersonTemplate}">
</TreeViewItem>
</TreeViewItem>
</TreeView>
This is a follow up question to binding-a-treeview-with-contextmenu-in-xaml 这是绑定-a-treeview-with-contextmenu-in-xaml的后续问题
This is a great way to get started using MVVM for treeview binding: 这是开始使用MVVM进行树视图绑定的好方法:
http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.