![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| Guest | 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 | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Expanded threads | C.B. | Live Mail | 3 | 02-12-2008 02:45 PM |
| Sub folder is not expanded? | JethroUK | Vista mail | 1 | 01-23-2008 06:04 PM |
| Expanded desktop?? | Charlie | Vista hardware & devices | 0 | 05-17-2007 09:36 PM |
| An error occurred whilst ensuring that your computer's machine-spe | =?Utf-8?B?VmlzdGEgQmxpc3RlcmVk?= | Vista General | 2 | 09-13-2006 07:48 AM |
| An error occurred whilst ensuring that your computer's machine-spe | =?Utf-8?B?VmlzdGEgQmxpc3RlcmVk?= | Vista General | 2 | 09-13-2006 01:49 AM |