【问题标题】:TreeView with grouping带分组的 TreeView
【发布时间】:2014-04-22 12:31:00
【问题描述】:

我需要在 TreeView 中为组创建重叠

类似的东西:

在我的 ItemsSource 中,节点 1、节点 1.1 和节点 2 组合在一起
并且将节点 3 和节点 3.1 分组在一起

将 CollectionView 与 PropertyGroupDescriptor 一起使用。

现在我不想在每个组周围添加橙色矩形

到目前为止,我的尝试是使用 GroupStyle,但这导致 2 个橙色 GroupItems 具有分组属性的值,我需要为组设置 GroupBox 的样式

有什么想法吗?

我的代码(这部分似乎与我无关,因为它只是我的分组逻辑,但如果某些人理解我所描述的内容很重要,我很乐意将它放在这里):

CS:

公共类 MyNode { 公共 int 组属性 { 得到;设置; }

    public string Title
    {
        get;set; 
    }

    public List<MyNode> Children
    {
       get;
       set;
    }

}

在 ViewModel 中:

 public List<MyNode> MyItemsSource
 {
     get
     {
          return new List<MyNode>
          {
               List<MyNode> nodes = new :ist<MyNode>();

               MyNode node1 = new MyNode{ Title = "Node1" ,GroupProperty = 1 }
               node1.Children.Add(new MyNode{ Title = "Node 1.1" , GroupProperty = 1});

               nodes.Add(node1)
               MyNode node2 = new MyNode{ Title = "Node1" ,GroupProperty = 2 }

               nodes.Add(node2)              

               ... same goes for Node 3 and Node 3.1 

               return nodes;
          };

     }

  }

XAML:

资源:

    <DataTemplate x:Key="protocolTemplate" DataType="{x:Type local:MyNode}">
        <TextBlock Text="{Binding Title}" />
    </DataTemplate>

    <HierarchicalDataTemplate x:Key="rootTemplate" DataType="{x:Type local:MyNode}" 
                              ItemTemplate="{StaticResource protocolTemplate}" ItemsSource="{Binding SubProtocols}">
        <TextBlock Text="{Binding Title}" />
    </HierarchicalDataTemplate>


    <CollectionViewSource x:Key="protocols" Source="{Binding Protocols}">
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="GroupProperty" />
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource> 

查看:

    <TreeView  ItemTemplate="{StaticResource rootTemplate}" ItemsSource="{Binding Source={StaticResource protocols}}" />

好吧,我添加了对我的 ItemsSource 进行分组的代码,我如何创建 GroupStyle 来在分组的项目上放置一种覆盖?

【问题讨论】:

  • 不会是ItemsPresenter吗?或者如果您使用样式ControlTemplate
  • 但这很难说我们是否没有任何代码可以使用。你能发布一些你尝试的代码吗?所以我们知道你采取了什么方法。
  • @XAMlMAX 这里的代码有点无意义,我根据我的 ItemsSource 的 Type 的某些属性执行分组,并且我有一个用于项目的 HierarchicalDataTemplate ,就是这样。这正是没有橙色矩形的绘图。

标签: wpf treeview grouping


【解决方案1】:

我找到了答案,我需要在 The GroupStyle 中做的所有事情 在我的 GroupItem 样式中放置了一个 ItemsPresenter,如下所示:

 <TreeView.GroupStyle>  
            <GroupStyle>                                        
                <GroupStyle.ContainerStyle>
                    <Style TargetType="GroupItem">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="GroupItem">
                                    <Grid>
                                        <Rectangle Fill="Orange"/>
                                        <ItemsPresenter />                                      
                                    </Grid>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
        </TreeView.GroupStyle>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多