Click here to Skip to main content
13,347,462 members (52,090 online)
Click here to Skip to main content
Add your own
alternative version


24 bookmarked
Posted 5 May 2009

WPF TreeView tools

, 5 May 2009
Rate this:
Please Sign up or sign in to vote.
Additional helper methods for the WPF TreeView (especially with data binding).


While working with the WPF TreeView and data binding, I ran into the problem that the TreeViewItems and my real objects were only loosely connected. That's why I decided to code some methods for the most important tasks like selecting, expanding, ... TreeViewItems that take my bound object as a parameter.

Using the Code

To use my small library, simply copy the file into your project. All the methods are implemented as extension methods for the TreeView or the TreeViewItem class.

The heart and soul of this library is the GetItemFromObject method:

/// <summary>
/// Returns the TreeViewItem of a data bound object.
/// </summary>
/// <param name="treeView">TreeView</param>
/// <param name="obj">Data bound object</param>
/// <returns>The TreeViewItem of the data bound object or null.</returns>
public static TreeViewItem GetItemFromObject(this TreeView treeView, object obj)
    DependencyObject dObject = GetContainerFormObject(treeView, obj);
    TreeViewItem tvi = dObject as TreeViewItem;
    while (tvi == null)
      dObject = VisualTreeHelper.GetParent(dObject);
      tvi = dObject as TreeViewItem;
    return tvi;
  catch { }
  return null;

private static DependencyObject 
        GetContainerFormObject(ItemsControl item, object obj)
  DependencyObject dObject = null;
  dObject = item.ItemContainerGenerator.ContainerFromItem(obj);
  if (dObject == null)
    if (item.Items.Count > 0)
      foreach (object childItem in item.Items)
        ItemsControl childControl = item.ItemContainerGenerator.
              ContainerFromItem(childItem)as ItemsControl;
        dObject = GetContainerFormObject(childControl, obj);
        if (dObject != null)
  return dObject;

By this, you get the TreeViewItem container of your object. Unfortunately, this method works only if the TreeViewNode with the object is visible (all parent nodes must be expanded). The GetContainerFromObject method is an internal helper to walk the VisualTree to find your object in the deeper tree levels. The VisualTreeHelper is the main reason why all parent nodes must be expanded.

All the following methods use this method to get the TreeViewItem, and then set or read the TreeViewItem's property.

public static void SelectObject(this TreeView treeView, object obj)
public static void SelectObject(this TreeView treeView, object obj, bool selected) 
public static bool IsObjectSelected(this TreeView treeView, object obj)
public static bool IsObjectFocused(this TreeView treeView, object obj)
public static void ExpandObject(this TreeView treeView, object obj)
public static void ExpandObject(this TreeView treeView, object obj, bool expanded)
public static bool IsObjectExpanded(this TreeView treeView, object obj)

The last method simply returns the parent TreeViewItem of a given TreeViewItem (can anyone tell me why this is missing in WPF or am I just blind!)

public static TreeViewItem GetParentItem(this TreeViewItem item)

Points of Interest

The TreeView shows me both sides of WPF: you get a super simple data binding with hierarchy, automatic updates, and so on, but on the other side, such simple things like walking a TreeView, expanding/collapsing items get a bit complex if you use data binding because of the super flexible Visual and Logical tree architecture.

I will extend this library in the future and update it from time to time. Any suggestions are welcome.


  • 05-05-2009: Original version.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Reinhard Ostermeier
Software Developer (Senior) remes GmbH
Germany Germany
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionThank you Pin
Member 883438716-Aug-12 7:34
memberMember 883438716-Aug-12 7:34 
QuestionWhat about collapsed tree? Pin
Sergey Zwezdin9-Nov-10 22:50
memberSergey Zwezdin9-Nov-10 22:50 
AnswerRe: What about collapsed tree? Pin
Reinhard Ostermeier4-Dec-10 22:44
memberReinhard Ostermeier4-Dec-10 22:44 
GeneralThanks! Pin
timtos27-Jun-09 15:13
membertimtos27-Jun-09 15:13 
GeneralRe: Thanks! Pin
timtos27-Jun-09 15:16
membertimtos27-Jun-09 15:16 
QuestionIs accessing UI items from code really good? Pin
disore14-May-09 21:12
memberdisore14-May-09 21:12 
GeneralPlease add a screenshot Pin
Member 189546112-May-09 4:30
memberMember 189546112-May-09 4:30 
I guess it would help new users to see at once the additional value, if you offer a screenshot too. Smile | :)
Rantthanks dude [modified] Pin
sachinblore5-May-09 18:49
membersachinblore5-May-09 18:49 
QuestionRe: thanks dude Pin
Reinhard Ostermeier6-May-09 4:41
memberReinhard Ostermeier6-May-09 4:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 5 May 2009
Article Copyright 2009 by Reinhard Ostermeier
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid