【问题标题】:WPF: Is there a built-in TreeGrid / TreeListView?WPF:是否有内置的 TreeGrid / TreeListView?
【发布时间】:2011-02-11 18:59:34
【问题描述】:

我需要这样的东西:

(我需要 TreeView 和 ListView 两个方面。即 Hirearchy 和 Columns。)

但是,我在 WPF 中需要它。这是内置的,还是我必须自己构建?

我认为它必须在框架中的某个地方,因为 VS2010 是在 WPF 中构建的。

编辑: 我已经设法获得了一些我想要使用 TreeView 和一些网格的功能,它们的列绑定到父网格的列,但是功能中有太多的怪癖。

编辑 2: 我仍然没有找到一种方法来做到这一点。有什么想法吗?

【问题讨论】:

    标签: wpf controls treelistview


    【解决方案1】:

    您正在寻找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>
    

    【讨论】:

    • 我需要多列。我已经尝试了大约 4 个小时从 TreeView 中哄骗多个列,但结果并不令人印象深刻。
    【解决方案2】:

    您可以使用 TreeView 模板中特别对齐的共享网格对象来伪造此显示...

    但我不相信您在 Visual Studio 中看到的实际上是 WPF 控件实现,它也存在于 Visual Studio 2008 中,并且可能是自定义本机控件或自定义 Windows 窗体控件。

    不过,好消息:如果您必须绝对拥有这种体验并很快想要它......这完全是一个 hack,但是:使用 Windows 窗体与您的 WPF 应用程序互操作。

    微软员工在 06 年发布了一个 winforms TreeGridView 实现的博客:

    【讨论】:

    • 我认为那是 WinForms。
    • 正确。我在上面已经指出,“这完全是 hack,但要使用 Windows 窗体与您的 WPF 应用程序互操作。”
    • 所有链接损坏
    【解决方案3】:

    ObjectListView 对我来说似乎很好......

    【讨论】:

    • ObjectListView 建立在 WinForms 之上,而不是 WPF
    • ObjectListView 可以在 WPF 中使用,但是当我在那里尝试时,有很多闪烁的问题,我在 Winforms 中没有相同的实现,所以我不能建议使用它。
    【解决方案4】:

    您可以通过关注this tutorial(在 ViewModel 的帮助下滚动 Fake 分组 部分)在一定程度上通过 DataGrid 获得这种行为。

    本教程的解决方案运行良好,但滚动可能会变得迟钝,并且当某些行折叠时它的行为不可预测。

    更新:我改变了孩子们的隐藏方式。我没有折叠行,而是删除了绑定的 ObservableCollection 中的项目。现在滚动效果很好!

    【讨论】:

      【解决方案5】:

      MSDN 上的这篇文章使用原生 WPF 来实现 TreeView/Grid 混合。它基于 TreeView 和 Grid 控件。它支持具有多列的树视图,但不支持排序或过滤

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

      编辑:我最近集成了这段代码,它工作得很好,给了你想要的东西:http://www.codeproject.com/Articles/30721/WPF-TreeListView-Control

      【讨论】:

        【解决方案6】:

        你考虑过 Xceed.Wpf.DataGrid 吗?

        您可以看到完整版 here的演示。

        还有一个社区版作为Extended WPF Toolkit™的一部分 - Ms-PL license

        List of the features in Full version
        List of the features in Community Edition
        不幸的是,我找不到表格样式的汇编。

        附言

        1. 通过在 Visual Studio 2010(专业版)上使用 Snoop(WPF Spy 实用程序)和 Spy++,我发现 您在 WatchLocalAutos 工具窗口中看到的 TreeGrid 称为 TREEGRID em>,它不是 Wpf 组件。 (但我不确定它是什么)。
          不过有趣的是,我发现 Breakpoints 工具窗口是通过并排使用两个组件构建的 - SysTreeView32SysListView32

        2. 我与 Xceed 没有任何关系 :-)

        编辑:
        似乎在这两个版本上都可以实现层次结构,但 Master-Detail 仅存在于 完整版 中,而在 社区版 你只能通过群组获取。
        :-(

        【讨论】:

        • 在 Visual Studio 2017 上,大部分 ToolWindow 都托管在 Microsoft.VisualStudio.Platform.WindowManagement.GenericPaneClientHwndHostMicrosoft.VisualStudio.Platform.WindowManagement.WindowFrame+ContentHostingPanel 但内部控件(树+网格)不是 真正的 WPF 控件。这些可能是存储在 Microsoft.VisualStudio.Platform.WindowManagement.dll 附近某处的一些内部(和原生?)控件
        【解决方案7】:

        这个对我来说就像一个魅力。 https://www.codeproject.com/Articles/30721/WPF-TreeListView-Control

        • 您使用 GetChildren 和 HasChildren 实现 ITreeModel。最好使用注册表示例检查示例代码以了解其完成方式。由于某种原因,开发人员忘记添加一个简单的示例......
        • 您必须自己向控件添加依赖属性才能使其与 MVVM 一起使用。所以它可能需要一些调整。将此添加到 TreeList.cs 以便能够绑定 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);
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-05-23
          • 2011-05-21
          • 1970-01-01
          • 2011-09-24
          • 1970-01-01
          • 2018-11-24
          相关资源
          最近更新 更多