Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

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.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > Avalon

Ensuring expanded

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 01-10-2006   #1 (permalink)
CSkinner
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 SpecsSystem Spec
Closed Thread

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


Vistax64.com 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 2005-2008

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 47 48 49 50 51