使用额外属性扩展TreeView?

时间:2016-08-25 16:48:49

标签: c# wpf xaml treeview

我正在为TreeView的扩展类添加一些属性。当单击树中的一个项目时,我需要额外的字段用于上下文。我似乎无法获得树视图来显示我给出的任何数据。 在我的MainView.cs中,我只是简单地设置项目来源:

TreeMenu.ItemsSource = (an ObservableCollection of ParentItems)

XAML:

    <Grid x:Name="TreeGrid" Width="350" HorizontalAlignment="Left">
        <TreeView Name="TreeMenu" Background="Red" Foreground="Black">
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate DataType="{x:Type model:ParentItem}" ItemsSource="{Binding ChildItems}">
                    <TextBlock Text="{Binding Text}" />
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>
    </Grid>

对象模型:

public class ParentItem : TreeViewItem, INotifyPropertyChanged 
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected void NotifyPropertyChanged(string info)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(info));
    }

    public ParentItem()
    {
        _text = "";
        _url = "";
        _childItems = new ObservableCollection<ChildItem>();
    }

    private string _text;
    public string Text
    {
        get
        {
            return _text;
        }
        set
        {
            _text = value;
            NotifyPropertyChanged("Text");
        }
    }

    private string _url;
    public string URL
    {
        get
        {
            return _url;
        }
        set
        {
            _url = value;
            NotifyPropertyChanged("URL");
        }
    }

    private ObservableCollection<ChildItem> _childItems;
    public ObservableCollection<ChildItem> ChildItems
    {
        get
        {
            return _childItems;
        }
        set
        {
            _childItems = value;
            NotifyPropertyChanged("ChildItems");
        }
    }
}

请注意,ChildItem几乎与ParentItem相同,减去了集合对象。最初我尝试在我的对象类中扩展TreeNode,但这有同样的问题。

有谁知道为什么我的TreeView不会出现?我在扩展TreeView时遗漏了什么?

1 个答案:

答案 0 :(得分:2)

延伸function $scope(meElement){ return obj = { write: function(txt){ return document.getElementById(meElement).innerHTML = txt; }, delete:function(){ return document.getElementById(meElement).innerHTML =''; }, color:function(col){ return document.getElementById(meElement).style.color = col ; } } } function init(){ $scope('games').write('hello world'); $scope('games').color('yellow'); } 没有意义。

我们没有看到您如何分配您的收藏,因此他们可能会做错事。

这确实有效:

<强>代码

TreeViewItem

<强> XAML

using System.Collections.ObjectModel;
using System.Windows;

namespace WpfApplication4
{
    /// <summary>
    ///     Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new ItemCollection
            {
                new Item
                {
                    Text = "A",
                    Items = new ItemCollection
                    {
                        new Item
                        {
                            Text = "B",
                            Items = new ItemCollection
                            {
                                new Item
                                {
                                    Text = "C"
                                }
                            }
                        }
                    }
                }
            };
        }
    }

    public class Item
    {
        public string Text { get; set; }

        public ItemCollection Items { get; set; }
    }

    public class ItemCollection : ObservableCollection<Item>
    {
    }
}

<强>结果

enter image description here