WPF中的TreeView绑定问题

时间:2010-03-23 22:49:26

标签: wpf xaml treeview

考虑以下数据结构:

List<Person> People;
class Person { 
  List<Car> Cars; 
  List<Hobby> Hobbies;
}

我想将TreeView绑定到此结构。它看起来应该是这样的:

People
> Frank
  > Cars
    > BMW
    > Ford
  > Hobbies
    > Tennis
    > Golf
> Jane
  > Cars
  > Hobbies

如何在XAML中实现这一目标?这是我到目前为止所得到的:

<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>

这是binding-a-treeview-with-contextmenu-in-xaml

的后续问题

1 个答案:

答案 0 :(得分:5)

这是开始使用MVVM进行树视图绑定的好方法:

http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx