views:

3125

answers:

4

Tell me it ain't so.

I have a typical windows/file explorer like setup.

  • Left Side I have a TreeView all data bound showing nodes in a hierachy
  • Right Side I have a ListView showing Node.Properties

ListView has a IsSynchronizedWithCurrentItem property, which rocks. e.g. If I had another ListView showing a list of nodes and both listViews have this property set to true. Changing selection of node in NodesListView will update the PropertiesListView automatically.

Now I need the same thing with a NodesTreeView and a PropertiesListView... and seems like TreeView has no such property.

Is there a more 'the WPF way' kind of solution to this problem ? Or do I have to handle the NodeSelectionChanged event of the Tree and refresh the listView via code.

A: 

My solution to this turned out to be pretty tiny.. don't know if it is equivalent to IsSynchronizedWithCurrentItem. ListView refreshes as expected.

// the XAML
<TreeView DockPanel.Dock="Left" x:Name="tvwNodes" ItemsSource="{Binding}" SelectedItemChanged="OnNewNodeSelected"/>
<ListView x:Name="lvwProperties" ItemsSource="{Binding Path=Properties}"

// the code-behind
private void OnNewNodeSelected(object sender, RoutedPropertyChangedEventArgs<object> e)
{
  lvwProperties.DataContext = tvwNodes.SelectedItem; // this returns the selected Node obj
}
Gishu
A: 

Why exactly it doesn't implement the property, I do not know, but i have a suggestion down below.

Your code above will work, however, it is not what the IsSynchronizedWithCurrentItem property does. Any ItemsControl binds to the ICollectionView of the ItemsSource property. To get that ICollectionView, we can call CollectionViewSource.GetDefaultCollectionView(object o). Depending on the type of object o, you get a different concrete implementation of the ICollectionView inteface. CollectionView and ListCollectionView are two concrete classes that come to mind.

The ICollectionView interface contains a member called CurrentItem. What the IsSynchronizedWithCurrentItem does is: whenever an item is clicked on the ItemsControl, it sets the CurrentItem for the collection view. The ICollectionView also has two events: CurrentItemChanging and CurrentItemChanged. When the IsSynchronizedWithCurrentItem property is set, the ItemsControl will update the SelectedItem based on what the ICollectionView's CurrentItem is. Makes sense?

In master/detail WPF scenarios, we simply are binding to ICollectionViews and their CurrentItem (the CurrentItem syntax is something like {Binding Items/Name}, where Name is the Name property on the collection's CurrentItem.

So although your code works for your purposes, it doesn't do what that property does. To do what the property does, you need to do the following:

  1. When an item is selected, you need to figure out which collection it belongs to. How do we do this? I believe this is why TreeView doesn't implement it. The selected item is displayed in a TreeViewItem. The DataContext is the object itself, but what is the parent collection ? I guess to get it you could either cache it in some hashmap (silly, but will work) or you can walk up the logical tree and get the TreeViewItem's parent that happens to be an ItemsControl. The ItemsSource property will be your collection.
  2. Get the ICollectionView for that collection.
  3. Need to cast that ICollectionView into a CollectionView (ICollectionView doesn't implement CurrentItem setter)
  4. Call SetCurrent(.. , ..) on the CollectionView instance

Now, anything that is bound to that ICollectionView's CurrentItem will be updated.

This became longer than I expected. Let me know if any further clarification is necesary.

siz
+2  A: 

A really simple solution is to bind your "details" UI elements to the SelectedValue property of the TreeView. For example, if your TreeView looked like this:

<TreeView Name="CategoryName" ItemsSource="{Binding Source={StaticResource A_Collection}, Path=RootItems}" />

Then you could bind details UI elements (like a textbox) using:

<TextBox Text="{Binding ElementName=CategoryTreeView, Path=SelectedValue.Name}"/>

Would cause the text box to be bound to Name property of the items currently selected in the TreeView.

If you want to bind many UI items as details for the selected TreeView item, consider setting up a DataContext on the elemtent that contains all the details controls (DockPanel / Grid / StackPanel, etc).

Mark
+1  A: 
<ListView Name="listView1" 
    ItemsSource="{Binding Path=SelectedItem.Modules, 
                          ElementName=treeView1, Mode=OneWay}" 
    IsSynchronizedWithCurrentItem="True">

Where ".Modules" is the collection of child items off the selected treeview item you want to display. Don't worry about wiring up the "SelectedItemChanged" event on the treeview.

Ross Oliver