none
Custom TreeViewItem? RRS feed

  • Question

  • I'm thinking about creating a custom TreeViewItem (or at least just extending TreeViewItem to implement some custom properties).  I want to add a checkbox and counter labels etc - my question is however, how do I get my tree to use the custom TreeViewItem rather than the default TreeViewItem object?

    Thursday, May 15, 2008 6:05 PM

Answers



  • This should do the trick

    class MyTreeView : TreeView
        {
            protected override DependencyObject GetContainerForItemOverride()
            {
                return new MyTreeViewItem();
            }

            protected override bool IsItemItsOwnContainerOverride(object item)
            {
                return item is MyTreeViewItem;
            }
        }

        class MyTreeViewItem : TreeViewItem
        {
            protected override DependencyObject GetContainerForItemOverride()
            {
                return new MyTreeViewItem();
            }

            protected override bool IsItemItsOwnContainerOverride(object item)
            {
                return item is MyTreeViewItem;
            }
        }

    P.S you have to do this for the MyTreeViewItem because the child item also creates items (a tree view is recursive)

    Hope it helps
    Thursday, May 15, 2008 8:08 PM

All replies



  • This should do the trick

    class MyTreeView : TreeView
        {
            protected override DependencyObject GetContainerForItemOverride()
            {
                return new MyTreeViewItem();
            }

            protected override bool IsItemItsOwnContainerOverride(object item)
            {
                return item is MyTreeViewItem;
            }
        }

        class MyTreeViewItem : TreeViewItem
        {
            protected override DependencyObject GetContainerForItemOverride()
            {
                return new MyTreeViewItem();
            }

            protected override bool IsItemItsOwnContainerOverride(object item)
            {
                return item is MyTreeViewItem;
            }
        }

    P.S you have to do this for the MyTreeViewItem because the child item also creates items (a tree view is recursive)

    Hope it helps
    Thursday, May 15, 2008 8:08 PM
  • This is a very nice solution! I wanted to cast my TreeViewItem to CustomTreeViewItem; but of course that was not possible. So how do you show it here, it worked great for me! Thanks!
    Thursday, October 2, 2014 10:40 AM
  • Hello,

    With above solution, I'm getting following error message.

    System.Windows.Data Error: 26 : ItemTemplate and ItemTemplateSelector are ignored for items already of the ItemsControl's container type; Type='MyTreeViewItem'

    May be its because of update of framework?

    Can you help?

    <TraceTree.xaml>

    <UserControl x:Class="stere_Excel_Add_in_Trace.WPF.TraceTree2.TraceTree" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:t="clr-namespace:stere_Excel_Add_in_Trace.WPF.TraceTree2" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"> <Grid x:Name="gridTop"> <Grid.RowDefinitions> <RowDefinition/> </Grid.RowDefinitions> <DockPanel Grid.Row="0"> <t:MyTreeView x:Name="treeView" Margin="0,0,0,0" ScrollViewer.HorizontalScrollBarVisibility="Auto"> <t:MyTreeView.ItemTemplate> <HierarchicalDataTemplate ItemsSource="{Binding Items}" DataType="{x:Type t:MyTreeViewItem}"> <StackPanel Orientation="Horizontal" VerticalAlignment="Top"> <TextBlock Text="{Binding Name}"/> </StackPanel> </HierarchicalDataTemplate> </t:MyTreeView.ItemTemplate> </t:MyTreeView> </DockPanel> </Grid></UserControl>

    <TraceTree.xaml.cs>

    using System.Windows; using System.Windows.Controls; using my.office.excel; namespace stere_Excel_Add_in_Trace.WPF.TraceTree2 { public partial class TraceTree:UserControl { public TraceTree(fnce.TraceDirection traceDirection) { InitializeComponent(); this.Initialize(); } private void Initialize() { //// Populate First Node treeView.Items.Clear(); MyTreeViewItem tviRoot = new MyTreeViewItem(); tviRoot.Name = "Test"; treeView.Items.Add(tviRoot); } } public class MyTreeView: TreeView { protected override DependencyObject GetContainerForItemOverride() { return new MyTreeViewItem(); } protected override bool IsItemItsOwnContainerOverride(object item) { return item is MyTreeViewItem; } } }

    <MyTreeViewItem.cs>

    using System.Windows; using System.Windows.Controls; namespace stere_Excel_Add_in_Trace.WPF.TraceTree2 { public class MyTreeViewItem: TreeViewItem { protected override DependencyObject GetContainerForItemOverride() { return new MyTreeViewItem(); } protected override bool IsItemItsOwnContainerOverride(object item) { return item is MyTreeViewItem; } public MyTreeViewItem() { } } }



    Sunday, October 13, 2019 2:58 PM