WPF TreeView 如何为TreeViewItem的子元素添加TreeViewItem控件模板

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15704761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:23:03  来源:igfitidea点击:

WPF TreeView how to add TreeViewItem control template for child elements of TreeViewItem

c#wpfmvvmtreeviewtreeviewitem

提问by 0xDEAD BEEF

how do I create treeview like this one:

我如何创建这样的树视图:

 <TreeViewItem Header="Customers" ItemsSource="{Binding Customers}">
  • Customers
    • Anna
      • Delete
      • Open
    • Peter
      • Delete
      • Open
    • Andrew
      • Delete
      • Open
  • 顾客
    • 安娜
      • 删除
      • 打开
    • 彼得
      • 删除
      • 打开
    • 安德鲁
      • 删除
      • 打开

I would like to create child item template something like this

我想创建这样的子项模板

 <TreeViewItem Header="{Binding Header}">
   <TreeViewItem Header="Delete"/>
   <TreeViewItem Header="Open"/>
 </TreeViewItem>

But it does not quite work that well because I end up having treeviewitem with datatemplate treeviewitem, but I would like to override controltemplate of child elements, but not parent. Sure, I want to avoid my binding to be TreeViewItem, nor I want to create children with those static obejct "Open", "Delete".

但它并不能很好地工作,因为我最终拥有带有 datatemplate treeviewitem 的 treeviewitem,但我想覆盖子元素的 controltemplate,而不是父元素。当然,我想避免我的绑定成为 TreeViewItem,也不想用那些静态对象“打开”、“删除”创建子对象。

回答by Pavel Voronin

Hereis one of the best articles about TreeViewI ever read.

TreeView我读过的最好的文章之一。

Inside TreeView.Resourcesyou could declare several DataTemplates with different DataTypeif Delete and Open commands were items of some collection. (TargetType for the commands would be ICommand).

如果删除和打开命令是某个集合的项目,则在内部TreeView.Resources您可以声明多个不同的 DataTemplates DataType。(命令的 TargetType 将是 ICommand)。

But it seems to mee you do not need TreeView at all. Customers is a header of the list. If you want it to be epxpandable use Expandercontrol.
Then it is sufficient to provide one data template for each customer.

但在我看来,您根本不需要 TreeView。客户是列表的标题。如果您希望它是可扩展的,请使用Expander控件。
那么为每个客户提供一个数据模板就足够了。

<DataTemplate DataType="CustomerTypeName">
    <Expander Header="{Binding CustomerName}">
        <Button Command="{Binding DeleteCustomerCmd}" Content="Delete" Margin="15,0,0,0"/>
        <Button Command="{Binding OpenCustomerCmd}" Content="Open" Margin="15,0,0,0"/>
    <Expander/>
<DataTemplate>

But here you'll have some troubles with selection highlight.

但是在这里您会遇到选择突出显示的一些麻烦。

public class CommandWrapper
{
    ICommand Command {get;set;}
    string CommandName {get;set;}
}

public class CustomerViewModel
{
    Customer Customer {get;set;}
    IEnumerable<CommandWrapper> Commands {get;}
}

Let Customers be collection of CustomerViewModel. Then the following XAML can help:

让客户成为收藏CustomerViewModel。那么以下 XAML 可以提供帮助:

<TreeView ItemsSource="{Binding ...}">
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="TypeHoldingCustomersCollection" 
            ItemsSource="{Binding Customers}">
            <TextBlock Text="Customers"/>
        </HierarchicalDataTemplate>

        <HierarchicalDataTemplate DataType="CustomerViewModel" 
            ItemsSource="{Binding Commands}">
            <TextBlock Text="{Binding Path=Customer.Name}"/>
        </HierarchicalDataTemplate>

        <DataTemplate DataType="CommandWrapper">
            <Button Content="{Binding CommandName}" Command="{Binding Command}"/>
        </DataTemplate>
    </TreeView.Resources>
</TreeView>

回答by Davio

You will want to use a HierarchicalDataTemplate for this.

为此,您将需要使用 HierarchicalDataTemplate。

Look here for a step-by-step tutorial

看看这里一步一步的教程