WPF Treeview - 绑定到具有不同深度的集合,并且样式不同

时间:2010-10-19 18:09:41

标签: wpf treeview

长篇大论的道歉 - 阅读有关此问题的几个主题,但仍然难以实施。基本上,我有一组对象,定义为:

public class LibData
{
    public string Name { get; set; }
    ObservableCollection<LibObject> _list;
    public ObservableCollection<LibObject> List { get { return _list; } }

    public LibData(string name, LibDataType type)
    {
        this.Name = name;
        _list = new ObservableCollection<LibObject>();
    }
}

和对象:

public class LibObject
{
    public string Name { get; set; }

    public LibObject(string name)
    {
        this.Name = name;
    }
}

我的主要问题在于XAML,并为这个TreeView设计样式。我需要为“root”项具有特定样式,并为“leaf”指定特定样式。事实上,绑定列表中的一个项目是“Root-&gt; Leaf”,另一个是“Root-&gt; Child-&gt; Leaf”。 我试过这个:

<TreeView x:Name="myTree" ItemsSource="{x:Static local:myDataList}">
     <TreeView.ItemTemplate>
         <HierarchicalDataTemplate ItemsSource="{Binding Path=List}" >
             <Grid>
                 <StackPanel Orientation="Horizontal">
                      <TextBlock Text="{Binding Path=Name}" />
                      <CheckBox IsChecked="True" Content="HeaderCheckbox"/>
                 </StackPanel>
             </Grid>
         <HierarchicalDataTemplate.ItemTemplate >
              <DataTemplate>
                  <Grid>
            <StackPanel Orientation="Horizontal">
                <CheckBox IsChecked="True" Content="LeafCheckbox" />
                <TextBlock Text="{Binding Path=Name}"/>
            </StackPanel>
                  </Grid>
              </DataTemplate>
          </HierarchicalDataTemplate.ItemTemplate>
      </HierarchicalDataTemplate>
  </TreeView.ItemTemplate>

这显然适用于“Root-&gt; Leaf”项目,但不适用于“Root-Child-Leaf”。 XAML实现似乎更像是一个“硬编码”解决方案,我知道项目布局总是“Root-&gt; Leaf” - 我该如何使其动态化? 再一次,我已经看到了不同级别的解决方案(包括使用转换器),但我遇到的问题是我需要根和叶子的特定样式,而不需要其间的级别。 我想知道我是否完全错了......

1 个答案:

答案 0 :(得分:6)

简单的方法来做到这一点。将DataTemplates从TreeView中拉出并将它们放在资源部分中。为每个DataTemplate指定DataType属性,但不包括键。 TreeView上的ItemTemplateSelector(DataTemplateSelector类型的属性)将使用适合正确项目类型的DataTemplate。“

为Root和Child类型创建HierarchicalDataTemplate,为Leaf类型创建DataTemplate。

这样的事情:

<Window.Resources>
    <ResourceDictionary>

        <DataTemplate DataType="{x:Type local:Leaf}">
            <Grid>
                <StackPanel Orientation="Horizontal">
                    <CheckBox IsChecked="True" Content="LeafCheckbox" />
                    <TextBlock Text="{Binding Path=SomeValue}"/>
                </StackPanel>
            </Grid>
        </DataTemplate>

        <HierarchicalDataTemplate DataType="{x:Type local:Child}"
                                  ItemsSource="{Binding Children}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Child " />
                <TextBlock Text="{Binding Path=SomeValue}" />
            </StackPanel>
        </HierarchicalDataTemplate>

        <HierarchicalDataTemplate DataType="{x:Type local:Root}"
                                  ItemsSource="{Binding Children}">
            <Grid>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="Root " />
                    <TextBlock Text="{Binding Path=SomeValue}" />
                </StackPanel>
            </Grid>
        </HierarchicalDataTemplate>

    </ResourceDictionary>
</Window.Resources>

<Grid>
    <TreeView x:Name="myTree" ItemsSource="{Binding}" />
</Grid>