none
Is it possible to replace a TreeViewItem of a TreeView RRS feed

  • Question

  • Hi,

    I want to replace the normal TreeViewItem of a TreeView with my own implementation, which, however, is derived from TreeViewItem. Is this possible?
    I've found ways to replace the Style or the ControlTemplate of the TreeViewItem via ItemContainerStyle and ItemTemplate, but no way to replace the TreeViewItem-Object itself.

    Greetings
    Cheesenhomer

    Wednesday, July 12, 2006 9:31 AM

Answers

  • why do you want to replace the standard TreeViewItem? what scenario do you want to enable?
        I use to have such a thought, but Ian Griffiths beats me to the ground.
        To answer your original question, if you really want to do it, you can use the some code like the following:
    public class MyTreeViewItem: TreeViewItem
    {
    }
    public class MyTreeView: TreeView:
    {
        protected override Boolean IsItemItsOwnContainerOverride (Object item)
        {
            return (item is MyTreeViewItem);
        }

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

    probably you have to do some additional work to get the code working, such as messing up with ItemContainerGenerator, I am not sure really.

    Sheva

    Wednesday, July 12, 2006 12:40 PM

All replies

  • can you give a scenario?
    Wednesday, July 12, 2006 12:10 PM
    Moderator
  • why do you want to replace the standard TreeViewItem? what scenario do you want to enable?
        I use to have such a thought, but Ian Griffiths beats me to the ground.
        To answer your original question, if you really want to do it, you can use the some code like the following:
    public class MyTreeViewItem: TreeViewItem
    {
    }
    public class MyTreeView: TreeView:
    {
        protected override Boolean IsItemItsOwnContainerOverride (Object item)
        {
            return (item is MyTreeViewItem);
        }

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

    probably you have to do some additional work to get the code working, such as messing up with ItemContainerGenerator, I am not sure really.

    Sheva

    Wednesday, July 12, 2006 12:40 PM
  • That's it! Thanks Sheva.
    I've created a TextBlock with an edit functionality and I want to use this Edit-TextBlock in a TreeView. When a TreeViewItem with such a Textblock inside is selected and I press F2, the TextBlock should switch to "editmode". The problem is that the TextBlock doesn't recieve the OnKeyDown-event because it isn't focusable, but the TreeViewItem is. So, my Solution is, that the TreeViewItem should know about the EditTextBlock to switch to "editmode".

    Thanks
    cheesenhomer
    Thursday, July 13, 2006 8:46 AM