【问题标题】:Hierarchical structure in treeview (C#)树视图中的层次结构(C#)
【发布时间】:2014-02-22 11:04:59
【问题描述】:

我想在树视图中可视化对象的层次结构。我知道有很多教程描述了如何做到这一点。原则上我想我什至知道该怎么做,但我被困住了。我希望有人能指出我的错误。

这是“我的对象”:

    private int _id;
    public virtual int Id
    {
        get
        {
            return this._id;
        }
        set
        {
            if(this._id != value)
            {
                this.OnPropertyChanging("Id");
                this._id = value;
                this.OnPropertyChanged("Id");
            }
        }
    }

    private string _name;
    public virtual string name
    {
        get
        {
            return this._name;
        }
        set
        {
            if(this._name != value)
            {
                this.OnPropertyChanging("name");
                this._name = value;
                this.OnPropertyChanged("name");
            }
        }
    }

    private int? _parentId;
    public virtual int? parentId
    {
        get
        {
            return this._parentId;
        }
        set
        {
            if(this._parentId != value)
            {
                this.OnPropertyChanging("parentId");
                this._parentId = value;
                this.OnPropertyChanged("parentId");
            }
        }
    }

    private MyObject _myObject1;
    public virtual MyObject MyParentObject
    {
        get
        {
            return this._myObject1;
        }
        set
        {
            if(this._myObject1 != value)
            {
                this.OnPropertyChanging("MyParentObject");
                this._myObject1 = value;
                this.OnPropertyChanged("MyParentObject");
            }
        }
    }

    private IList<MyObject> _myObjects = new List<MyObject>();
    public virtual IList<MyObject> MyChildObjects
    {
        get
        {
            return this._myObjects;
        }
    }

这里重要的是名为“MyChildObjects”的子对象列表。

XAML 如下所示:

    <TreeView ItemsSource="{Binding myObjects}">
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding myObjects/MyChildObjects}">
                <TextBlock Text="{Binding name}" />
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>

我现在的问题是树视图只显示所有对象的平面结构。错误最有可能出现在 XAML 文件中,但我无法弄清楚。我必须更改什么才能在树视图中显示层次结构?

感谢您的帮助! 最好的问候

【问题讨论】:

    标签: c# wpf xaml treeview


    【解决方案1】:

    尝试在TreeView.Resources 中为DataType of MyObject 定义您的HierarchicalDataTemplate

    <TreeView ItemsSource="{Binding myObjects}">
       <TreeView.Resources>
          <HierarchicalDataTemplate DataType="{x:Type local:MyObject}" ItemsSource="{Binding MyChildObjects}">
             <TextBlock Text="{Binding name}" />
          </HierarchicalDataTemplate>
       </TreeView.Resources>
    </TreeView>
    

    你的ItemsSource 路径也是错误的。当您使用myObjects/ 时,它表示myObjects 的当前项目。你需要的只是ItemsSource="{Binding MyChildObjects}

    Binding.Path:

    当源是集合视图时,当前项可以用斜杠 (/) 指定。例如,子句 Path=/ 将绑定设置为视图中的当前项。当源是集合时,此语法指定默认集合视图的当前项。

    【讨论】:

    • 在这种情况下是否需要DataType(树视图将包含单一类型的实例)?
    • 当您在Resources 中指定DataTemplate 时,您必须指定DataTypex:Key
    【解决方案2】:

    您已经设置了 ItemsSource,但我认为您还需要在 HierachicalDataTemplate 中设置 ItemTemplate。看看here

    【讨论】:

      猜你喜欢
      • 2012-03-11
      • 1970-01-01
      • 1970-01-01
      • 2013-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-18
      • 1970-01-01
      相关资源
      最近更新 更多