![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
| |
| | #1 (permalink) |
| | Ensuring expanded I'm using the HierarchicalDataTemplate to populate my TreeView. The downside to this is that myTreeView.SelectedItem returns the actual data and NOT the TreeViewItem object associated with the data. The problem with this is that it is difficult to set properties on the TreeViewItem associated with deeply nested data items. This is a real problem, as I'm am trying to ensure that new nodes generated under a parent are visible, so I'd like to set the parent's TreeViewItem.IsExpanded property to true. The trick is getting that TreeViewItem! This code works when creating nodes for the first / root items, but then second Debug.Assert trips when creating nodes for sub nodes: private void CheckSelectedItem( bool isChecked ) { Debug.Assert( mProjectTree != null ); object selectedItem = mProjectTree.SelectedItem; if( selectedItem != null && mProjectTree != null ) { TreeViewItem treeItem = mProjectTree.ItemContainerGenerator.ContainerFromItem( selectedItem ) as TreeViewItem; Debug.Assert( treeItem != null ); if( treeItem != null ) { treeItem.IsExpanded = isChecked; } } } I have tested some code that can walk up the parent hierarchy of my data in order to find the first ItemContainerGenerator from which to start walking down the tree, but that just seems way too difficult and cumbersome, so I must be doing something wrong! There is another case where this has caused me undo headaches. I wanted to ensure that a right click in the tree selected whatever node the user clicked on. Look at how complicated this code is! private void OnRightButtonDown( object sender, MouseEventArgs e ) { // All this code is needed in order to figure out what node // the user has right clicked over!!! if( e.MouseDevice.Target is FrameworkElement ) { FrameworkElement element = e.MouseDevice.Target as FrameworkElement; System.Windows.Controls.ContentPresenter pres = element.TemplatedParent as ContentPresenter; if( pres != null ) { TreeViewItem viewItem = pres.TemplatedParent as TreeViewItem; if( viewItem == null ) { viewItem = mProjectTree.ItemContainerGenerator.ContainerFromItem( pres.Content ) as TreeViewItem; } if( viewItem != null ) { viewItem.IsSelected = true; } } } } There's gotta be an easier way! ![]() Thanks, CS |
My System Specs![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| non-expanded messages | Live Mail | |||
| Expanded threads | Live Mail | |||
| Sub folder is not expanded? | Vista mail | |||
| An error occurred whilst ensuring that your computer's machine-spe | Vista General | |||
| An error occurred whilst ensuring that your computer's machine-spe | Vista General | |||