开发者

How can I change my treeview's visual to automatically display a SelectedItem? [duplicate]

开发者 https://www.devze.com 2023-03-30 08:16 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: WPF TreeView - How to scroll so expanded branch is visible
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

WPF TreeView - How to scroll so expanded branch is visible

I'm developing an application that utilizes the standard TreeView functionality in WPF. I've created a search function within the TreeView that will parse through the tree and locate a matching title (e.g. "Create" will work for finding a TreeViewItem titled "Create a Project:". If I press the "enter" key while the search textbox still has focus, it'll parse through the tree for the next possible TreeViewItem. The search functionality is inspired by the code found here at Josh Smith's TreeView Article, but modified slightly to work within the resources of my application and its nodes (the trees generated are dynamic, depending on the files imported in the application, and I obviously have my own Object classes).

When I tried to look up this issue, I found a million and one search hits for how to set the SelectedItem for the TreeView (which I'm not having an issue with), but I couldn't find anything specifically for my problem which is this: Every time I do a search, the tree will find the SelectedItem (if it exists), and call IsExpanded = true to all the elements recursively up to the root of the tree. But sometimes the element discovered is outside of the visual frame of the TreeView (I have scrollbars set to auto, so when the tree grows too large horizontally or vertically, scroll bars are enabled). The SelectedItem changes appropriately, but I have to scroll down the TreeView in order to find where my match was made. What I'm hoping to do is fire an event that will detect when the SelectedItem is changed, and then force the Visu开发者_开发技巧al of the TreeView to show the selected item at some point within its visible frame (so it's never out of visibility if found), but I'm not sure how exactly to do this (my experience with WPF is limited to the last month or so I've spent developing this application, so I'm still new to the language and picking things up as I go along).

Are there any solutions for this? I can provide code if desired, but I'm just looking for somebody to point me in the right direction on how to change the visible region of my TreeView item.


You need to call BringIntoView on the selected TreeViewItem. To not pollute your ViewModel with this code, you could create an Attached Property that calls this method when it is set to true and bind it to the IsSelected property.

Another way is to use a trigger on the IsSelected property to call this method as shown here.


You can use a simple EventSetter in TreeViewItem style to invoke an event handler when the item is selected. Then call BringIntoView for the item.

<TreeView >
 <TreeView.ItemContainerStyle>
   <Style TargetType="{x:Type TreeViewItem}">
     <EventSetter Event="Selected" Handler="TreeViewSelectedItemChanged" />
   </Style>
 </TreeView.ItemContainerStyle>

</TreeView>

private void TreeViewSelectedItemChanged(object sender, RoutedEventArgs e)
{
    TreeViewItem item = sender as TreeViewItem;
    if (item != null)
    {
        item.BringIntoView();
        e.Handled = true;  
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消