tags:

views:

1284

answers:

3

Hi,

I'm certain this has come up before, but I haven't been able to find the answer.

I made a basic ViewModel that contains a list of People (an array of Person) with a property called SelectedPerson, which naturally points to the currently selected Person in the list of People. I also have a ListBox and a TreeView that are databound to the ViewModel's People list.

What I'd like to do is to keep the ListBox's SelectedValue and TreeView's SelectedItem in sync with with the ViewModel's SelectedPerson. The idea is that no matter how the SelectedPerson is modified (through a control, through code, etc), all the controls should update themselves properly. I can get it to work with two ListBoxes, which is nice, but I can't get it to work with a ListBox and a TreeView because the TreeView's SelectedItem is readonly and apparently unavailable through XAML.

Where should I look to get ideas on making this work?

Also note that I'm trying to make this work in pure XAML. No code-behind as XAML files in my application can be loaded and changed dynamically.

Thanks!

+2  A: 

You can Use Selector.IsSyncronizedWithCurrentItem.

You can bind both thye listbox and treeview to the same datasource and make sure that the IsSyncronized parameter is set to true. Then any changes to the current item in one will be reflected in the other.

More information can be found here:

link text

midas06
Hi Midas! I looked into your suggestion, but it looks like TreeView doesn't have that property.
Steve the Plant
A: 

I asked around and the best solution I could find was here

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/cc73893a-3383-4328-a002-ed8fb002a19d

It works for me but it's not the most optimal solution at this point.

Steve the Plant
A: 

Hi, this mechanism works only for the inherited UIElement of ItemsControl(ListBox, ThreeView etc) not for ItemsControl self.

 <TreeView.ItemContainerStyle>       
    <Style TargetType="{x:Type TreeViewItem}">
       <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
    </Style>
</TreeView.ItemContainerStyle>

Can we do this for ItemsControl?

<ItemsControl.ItemContainerStyle>       
    <Style TargetType="{x:Type ItemsControl}">
       <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
    </Style>
</ItemsControl.ItemContainerStyle>

It will give error on x:Type ItemsControl? Can you suggest something?

Naresh Goradara