wpf - 对树的不同级别使用不同的模板

标签 wpf mvvm triggers treeview

我想显示绑定(bind)到模型的 TreeView 它有效但是:

<TreeView ItemsSource="{Binding Items}">
            <TreeView.ItemContainerStyle>
                <Style TargetType="{x:Type TreeViewItem}" >
                    <Style.Triggers>
                        <Trigger Property="HasItems" Value="true">
                            <Setter Property="Focusable" Value="False"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </TreeView.ItemContainerStyle>
                <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding SubNodes}">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="20"/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <CheckBox Grid.Column="0"/>
                        <TextBlock Text="{Binding Text}" Grid.Column="1"/>
                    </Grid>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>

我想要三个模板:一个用于 rootitem ,第二个用于子节点,第三个用于子节点但可以替代另一个模板。

最佳答案

相对简单。

定义树绑定(bind)的三种不同类型:

// defines root nodes in the tree
public sealed class RootNode : ITreeNode // or some other interface or base type
{
    public IEnumerable<SubNode> SubNodes {get;set;}
}

//  defines all middle nodes
public class SubNode : ITreeNode
{
   public IEnumerable<SubNode> Children {get;set;}
}

// defines leafs
public sealed class LeafNode : SubNode { }

构造它们并将它们添加到您的 ViewModel

public sealed class ViewModel 
{
    // or use an OC<T> or whatever your design needs
    public IEnumerable<RootNode> Roots {get;set;}

然后使用HierarchicalDataTemplate.DataType为每个指定模板:

<TreeView
  ItemsSource="{Binding Roots}">
  <TreeView.Resources>
      <HierarchicalDataTemplate 
          xmlns:t="clr-namespace:NamespaceForNodeClasses"
          DataType="{x:Type t:RootNode}
          ItemsSource="{Binding SubNodes}">
          <TextBlock Text="I'm a root node!"/>
      </HierarchicalDataTemplate>
      <HierarchicalDataTemplate 
          xmlns:t="clr-namespace:NamespaceForNodeClasses"
          DataType="{x:Type t:SubNode}
          ItemsSource="{Binding Children}">
          <TextBlock Text="I'm a regular tree node!"/>
      </HierarchicalDataTemplate>
      <HierarchicalDataTemplate 
          xmlns:t="clr-namespace:NamespaceForNodeClasses"
          DataType="{x:Type t:LeafNode}>
          <TextBlock Text="I'm a leaf!"/>
      </HierarchicalDataTemplate>
  </TreeView.Resources>
</TreeView>

关于wpf - 对树的不同级别使用不同的模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7284804/

相关文章:

wpf - 增加 TreeView 中 TreeViewItems 之间的间距

wpf - 将 Caliburn Micro Screen(UserControl) 添加到 WPF MVVM 中的 Canvas ?

c# - 使用 MVVM 将命令绑定(bind)到 DataTemplate 中的控件

wpf - WPF 的 "nice"特性真的那么好吗?还是他们控制欲太强?

android - 无法使用数据绑定(bind) Android 从 ViewModel 与 XML 通信

c# - 死掉的简单 MVVM 应用程序的列表框保持为空 - 我错过了什么?

jquery - 如何在Jquery中检测div上的长按?

MySQL - 当表 1 有新记录时,如何使用 MySQL 工作台在表 2 下创建子记录?

oracle - 指定后语句时如何检查 :new value in compound triggers,?

wpf - 如何设置按钮的内部文本边距