【问题标题】:WPF TreeView - group list of itemsWPF TreeView - 项目组列表
【发布时间】:2016-09-20 10:39:04
【问题描述】:

我有一个类似这样的类列表:

class MyClass
{
   string Group {get;set;}
   string SubGroup {get;set;}
   string Item {get;set;}
}

在我的树视图中,我希望将列表分组为:

组1
...子组1
......项目1
......第2项
...子组2
......项目3
...子组 3
......Item4
......项目5
组2
...子组4
......第6条

我的 xaml 应该是什么样子?

我尝试过嵌套的 HierarchicalDataTemplates、GroupStyle 和 CollectionViewSource,但似乎没有任何效果......

另外,如果能够编辑项目属性,那就太好了。

编辑:被称为Grouping child objects in WPF TreeView 的副本,但似乎这个家伙开始时是我想要结束的(有点)

【问题讨论】:

标签: c# wpf xaml treeview


【解决方案1】:

一个很好的代码-撒玛利亚人写了这个。我会把它贴在这里,以防其他人觉得它有用:

    <DataTemplate x:Key="LeafTemplate">
        <!--your item's property-->
        <TextBlock Text="{Binding Path=Val3}"/>
    </DataTemplate>

    <HierarchicalDataTemplate ItemsSource="{Binding Items}" x:Key="Level2GroupTemplate" ItemTemplate="{StaticResource LeafTemplate}">
        <!--GroupItem.Name-->
        <TextBlock Text="{Binding Path=Name}" />
    </HierarchicalDataTemplate>

    <HierarchicalDataTemplate ItemsSource="{Binding Items}" x:Key="Level1GroupTemplate"  ItemTemplate="{StaticResource Level2GroupTemplate}">
        <!--GroupItem.Name-->
        <TextBlock Text="{Binding Path=Name}" />
    </HierarchicalDataTemplate>

</Window.Resources>

    public ObservableCollection<TestTreeClass> TestTreeList
    {
        get { return (ObservableCollection<TestTreeClass>)GetValue(TestTreeListProperty); }
        set { SetValue(TestTreeListProperty, value); }
    }

    // Using a DependencyProperty as the backing store for TestTreeList.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty TestTreeListProperty =
        DependencyProperty.Register("TestTreeList", typeof(ObservableCollection<TestTreeClass>), typeof(MainWindow), new PropertyMetadata(null));

    TestTreeList = new ObservableCollection<TestTreeClass>()
        {
            new TestTreeClass() { Val1 = "AAA", Val2 = "111", Val3 = "abc" },
            new TestTreeClass() { Val1 = "AAA", Val2 = "111", Val3 = "def" },
            new TestTreeClass() { Val1 = "BBB", Val2 = "111", Val3 = "ghi" },
            new TestTreeClass() { Val1 = "BBB", Val2 = "111", Val3 = "jkl" },
            new TestTreeClass() { Val1 = "AAA", Val2 = "222", Val3 = "mno" }
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-25
    • 2010-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多