【问题标题】:Extending TreeView with Extra Properties?用额外的属性扩展 TreeView?
【发布时间】:2016-08-25 16:48:49
【问题描述】:

我正在为 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");
        }
    }
}

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

有人知道为什么我的 TreeView 不会出现吗?扩展 TreeView 时是否遗漏了什么?

【问题讨论】:

    标签: c# wpf xaml treeview


    【解决方案1】:

    扩展TreeViewItem没有意义

    我们看不到您如何分配您的收藏,因此他们可能做错了什么。

    这确实有效:

    代码

    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>
        {
        }
    }
    

    XAML

    <Window x:Class="WpfApplication4.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApplication4"
            mc:Ignorable="d"
            Title="MainWindow" Height="350" Width="525">
    
        <Grid>
            <TreeView ItemsSource="{Binding}">
                <TreeView.ItemTemplate>
                      <HierarchicalDataTemplate DataType="local:Item" ItemsSource="{Binding Items}">
                    <TextBlock Text="{Binding Text}" />
                   </HierarchicalDataTemplate>
                </TreeView.ItemTemplate>
            </TreeView>
        </Grid>
    </Window>
    

    结果

    【讨论】:

    • 完美,我最终改变的只是没有在我的类中扩展 TreeViewItem。这似乎是问题所在。谢谢!
    猜你喜欢
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-10
    相关资源
    最近更新 更多