views:

45

answers:

1

Hi All,

In my WPF application I have a treeview which is loaded with certain collection. At the top of this tree view control i have a listbox which stores the same items as in the tree view whenever the tree view items are made favorites. I am trying to focus the treeview item whenever there is a selection made in the listbox. Though the relevant item is getting selected in the tree view whenever i select an item in the listbox, but I am not able to focus the treeview item. Since the tree view is huge, there is a vertical scrollbar and if the item selected in the listbox falls in the non visible part of the tree view, I want the scrollbar to be set so that the treeview item gets selected. Though I am calling Focus explicitly on the selected treeviewitem, I am unable to see the item in the visible pane of the treeview.

Could anyone please help me in achieving the above behavior?

Here is the code I am using from Josh Smith's article on attachedbehaviors:

        `static void OnTreeViewItemSelected(object sender, RoutedEventArgs e) 
    { 
             if (!Object.ReferenceEquals(sender, e.OriginalSource)) return; 
           TreeViewItem item = e.OriginalSource   as TreeViewItem;
        if (item != null) item.BringIntoView(); 
     }

`
Thanks!

A: 

Have you tried

treeViewItem.BringIntoView();

?

If you still haven't solved this you should try

static void OnTreeViewItemSelected(object sender, RoutedEventArgs e)  
{  
    if (!Object.ReferenceEquals(sender, e.OriginalSource))
    {
        return;
    }
    TreeViewItem item = e.OriginalSource   as TreeViewItem; 
    if (item != null)
    {
        // Do you have access to your TreeView here?
        // If it's called myTreeView..
        EventHandler eventHandler = null;
        eventHandler = new EventHandler(delegate
        {
            myTreeView.LayoutUpdated -= eventHandler;
            item.BringIntoView();
        });
        myTreeView.LayoutUpdated += eventHandler;
    }
} 
Meleak
Hi Meleak, I tried BringIntoView(), but it did not work for me...any clue?
Sowmya
Do you have any sample code?
Meleak
Yep. I am doing similar to what is suggested in the below Josh Smith's article too, but yet no luck... http://www.codeproject.com/KB/WPF/AttachedBehaviors.aspx I have a viewmodel associated with the treeviewitems and whenever the IsSelected property of the treeitemviewmodel changes to true i want BringToView to work. I tried but in vain..can you please help...
Sowmya