WPF: Is there a built-in TreeGrid / TreeListView?

前端 未结 7 1129
长情又很酷
长情又很酷 2020-12-29 21:05

I need something just like this:

(I need both the TreeView and the ListView aspects. That is, Hirearchy and Columns.)

But, I need it in WPF. is thi

相关标签:
7条回答
  • 2020-12-29 21:12

    You may be able to fake this display using specially-aligned shared Grid objects in your templates for TreeView...

    However I don't believe the one you see in Visual Studio is actually a WPF control implementation, it was there in Visual Studio 2008 as well and is likely either a custom native control or custom Windows Forms control.

    Good news, though: if you must absolutely have this experience and want it soon... it's a total hack, but: use Windows Forms interop with your WPF app.

    A Microsoft employee blogged a winforms TreeGridView implementation back in '06:

    • http://blogs.msdn.com/markrideout/archive/2006/01/08/510700.aspx
    • http://blogs.msdn.com/markrideout/archive/2006/01/18/new-link-for-treegridview.aspx
    0 讨论(0)
  • 2020-12-29 21:20

    You are looking for the TreeViewhttp://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.treeview.aspx:

    <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
        <Page.Resources>
            <XmlDataProvider x:Key="StaticXml" XPath="root/foo">
                <x:XData>
                    <root xmlns="">
                        <foo a="_File">
                            <foo a="New">
                                <foo a="_Project..." />
                                <foo a="_Web Site..."/>
                            </foo>
                            <foo a="C_lose"/>
                            <foo a="E_xit"/>
                        </foo>
                        <foo a="_Edit">
                            <foo a="Cu_t"/>
                            <foo a="_Copy"/>
                            <foo a="_Paste"/>
                        </foo>
                    </root>
                </x:XData>
            </XmlDataProvider>
            <HierarchicalDataTemplate x:Key="MenuTemplate" ItemsSource="{Binding XPath=foo}">
                <AccessText Text="{Binding XPath=@a}"/>
            </HierarchicalDataTemplate>
        </Page.Resources>
        <StackPanel>
            <TreeView
                    ItemsSource="{Binding Source={StaticResource StaticXml}}"
                  ItemTemplate="{StaticResource MenuTemplate}"/>
        </StackPanel>
    </Page>
    
    0 讨论(0)
  • 2020-12-29 21:23

    ObjectListView seems quite good to me...

    0 讨论(0)
  • 2020-12-29 21:25

    This post on MSDN makes use of native WPF to achieve a TreeView/Grid hybrid. It is based on the TreeView and Grid controls. It supports a treeview with multiple columns, but does not support sorting or filtering

    http://dlaa.me/blog/post/9898803

    EDIT: I have recently integrated this code and it works very nicely, gives you exactly what you want: http://www.codeproject.com/Articles/30721/WPF-TreeListView-Control

    0 讨论(0)
  • 2020-12-29 21:27

    This one works like a charm for me. https://www.codeproject.com/Articles/30721/WPF-TreeListView-Control

    • you implement ITreeModel with GetChildren und HasChildren. Best to check the example code with the registry example to understand how its done. For some reason the developer forgot to add a simple example...
    • you have to add dependency properties yourself to the control to get it to work with MVVM. So it may need some tweaking. Add this to TreeList.cs to be able to bind the TreeModel:

     

    public ITreeModel TreeModel
    {
        get => (ITreeModel)GetValue(TreeModelProperty);
        set => SetValue(TreeModelProperty, value);
    }
    
    public static readonly DependencyProperty TreeModelProperty =
        DependencyProperty.Register(
            "TreeModel",
            typeof(ITreeModel), 
            typeof(TreeList), 
            new PropertyMetadata(null, OnModelChanged));
    
    private static void OnModelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var treeList = (TreeList) d;
        treeList.Root.Children.Clear();
        treeList.Rows.Clear();
        treeList.CreateChildrenNodes(treeList.Root);
    }
    
    0 讨论(0)
  • 2020-12-29 21:28

    You can somewhat obtain this behavior with the DataGrid by following this tutorial (scroll the the Fake grouping with the help of the ViewModel section).

    The tutorial's solution works well enough, but scrolling can get laggy and it's behavior unpredictable when some rows are collapsed.

    Update: I changed how the children are hidden. Instead of collapsing rows, I removed items in the bound ObservableCollection. Now scrolling works perfectly!

    0 讨论(0)
提交回复
热议问题