【问题标题】:TreeView with inherited types具有继承类型的 TreeView
【发布时间】:2015-05-21 09:37:47
【问题描述】:

我想显示一个包含实现接口的元素的 TreeView。这个接口是由两个主要类实现的,就是我要展示的。

架构类似于:

IElement
    Container : IElement
      ->public IEnumerable<IElement> Elements {get; set;}
    Element : IElement

所以基本上,这个 TreeView 必须能够在任何级别上显示容器和元素。容器应该是“可扩展的”(因为它们包含其他 IElement),但元素不应该。

所以this solution 似乎不合适,因为它设置了两个完全不同的级别(企业/员工)。

我不知道如何使用 IElement 填充 TreeView,同时能够检查它们是容器还是元素,以及如何防止仅扩展其中一种类型。

【问题讨论】:

  • 测试元素是否为Container:if (element is Container) ...

标签: c# wpf xaml mvvm treeview


【解决方案1】:

这是否符合您的要求? 这是一个示例:

CS:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
    }

    public List<IElement> Elements
    {
        get
        {
            var list = new List<IElement>();

            list.Add(BuildContainer());
            list.Add(BuildContainer());
            list.Add(new Element());

            return list;
        }
    }

    private Container BuildContainer()
    {
        var container = new Container();

        container.Elements.Add(new Element());
        container.Elements.Add(new Element());

        var sub_container = new Container();
        sub_container.Elements.Add(new Element());

        container.Elements.Add(sub_container);

        return container;
    }
}

public interface IElement
{
     string Title { get; }
}

public class Container : IElement
{
    public string Title
    {
        get { return "Container"; }
    }

    private ObservableCollection<IElement> elements;
    public ObservableCollection<IElement> Elements
    {
        get
        {
            if (elements == null)
            {
                elements = new ObservableCollection<IElement>();
            }
            return elements;
        }
    }
}

public class Element : IElement
{
    public string Title
    {
        get { return "Element"; }
    }
}

XAML:

<Window
    xmlns:local="clr-namespace:WpfApplication1"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>

    <DataTemplate DataType="{x:Type local:Element}">
        <TextBlock Text="{Binding Title}" Foreground="Red" FontSize="14"/>
    </DataTemplate>

    <HierarchicalDataTemplate DataType="{x:Type local:Container}" ItemsSource="{Binding Elements}">
        <TextBlock Text="{Binding Title}" Foreground="Black" FontWeight="Bold" FontSize="16"/>
    </HierarchicalDataTemplate>
</Window.Resources>


<Grid>
    <TreeView ItemsSource="{Binding Elements}" />
</Grid>

结果:

【讨论】:

  • 别人做这件事看起来很容易。这正是我正在寻找的。它让我了解了在 TreeView 中使用 HierarchicalDataTemplate 和 DataTemplate。非常感谢。
  • 现在对您来说是一个挑战,假设您只有一个根节点并且您希望它看起来完全不同。
  • 我必须说我缺乏关于 TreeView 的知识。它似乎没有任何特定的访问根节点的样式;
  • @Kilazu 如果这对你有帮助,你应该排除答案
【解决方案2】:

向您的IElement interface 添加一个bool 类型的属性IsExpandable。将此属性设置为 true 仅适用于 Container 类,并在 BindingTrigger 中将此属性用于您的 TreeViewItem

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-13
    • 2021-09-21
    • 1970-01-01
    • 2016-02-09
    相关资源
    最近更新 更多