Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > Avalon

Vista - Ensuring expanded

 
 
Old 01-10-2006   #1 (permalink)
CSkinner


 
 

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 SpecsSystem Spec
 

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


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46