June 01, 2011

WPF SelectedValue for TreeView

Questions:
- How to programmatically select an Item in a WPF TreeView
- How to programmatically selecting an Item in a WPF TreeView
- How to SelectedItem in a WPF TreeView
- How to set SelectedItem or SelectedValue in C#/WPF TreeView
- WPF TreeView SelectedItem should have set accessor …
- How to programmatically change the SelectedItem in a WPF TreeView

Solution:
The Solution is simple and quick with just a little method
TreeViewItem matchItam = SelectTreeViewItem(this.treeView1.Items, "sample value");

The code is here
private TreeViewItem SelectTreeViewItem(ItemCollection Collection, String Value)
{
    if (Collection == null) return null;
    foreach(TreeViewItem Item in Collection)
    {
        /// Find in current
        if (Item.Header.Equals(Value))
        {
            Item.IsSelected = true;
            return Item;
        }

        /// Find in Childs
        if (Item.Items != null)
        {
            TreeViewItem childItem = this.SelectTreeViewItem(Item.Items, Value);
            if (childItem != null)
            {
                Item.IsExpanded = true;
                return childItem;
            }
        }
    }
    return null;
}

3 comments:

Anonymous said...

i dnt know how to wrk wid WPF treeview..

m trying to work on...wen user clicks any any item den how to show it on msgbox from root to parent den to child...

Anonymous said...

thnk u for ur consideration....

help me..

Amastaneh said...

Dear Anonymous,
call me on and tell me what you want, i'm really in hardworking these days for updating blog or likes, call me, any time