The Whether Man The Whether Man - 3 months ago 13
C# Question

Extending TreeView with Extra Properties?

I'm working on adding some properties to an extension class of TreeView. I need the extra fields for context when one of the items in the tree is clicked. I can't seem to get the tree view to show any of the data I'm giving it.
In my MainView.cs I'm simply setting the items source as such:

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>


Object model:

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");
}
}
}


Note that
ChildItem
is almost identical to
ParentItem
, minus the collection object. Originally I tried extending
TreeNode
in my object classes, but that had the same issue.

Does anyone know why my TreeView won't appear? Am I missing something while extending TreeView?

Answer

No point in extending TreeViewItem.

We don't see how you assign your collection so they might something done wrongly.

This does work:

Code

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>

Result

enter image description here

Comments