2

I have a Shell.xaml file which contains two other UserControls. On the left is my TreeView and on the right is a detail screen.

I want the detailscreen to be switchable based on a selected TreeViewItem. I know this can be achieved by using DataTemplates, because I've done it with simple button clicks and using the <ContentControl Content="{Binding CurrentDetailViewModel}"> tag to accomplish this, but I have no idea how to accomplish this based on a selected TreeViewItem. I also have a separate ViewModel class for my UserControl which holds my TreeView and a separate for each detail screen.

I've been using Josh Smith's tutorial on TreeViews: http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx

So I also do use the TreeViewItemViewModel.cs class of his.

Could someone shed some light onto this?

Thanks,

Grant

supercell
  • 309
  • 1
  • 4
  • 17

1 Answers1

1

If both the treeview and the details are displaying the same object (i.e the ItemsSource of the treeview contains the objects that you want to data template in the custom control) then you should be able to set a property on an underlying ViewModel that both controls share and have the custom control display something relevant with data templates.

for example, in the ViewModel:

object TreeViewSelectedItem
{
    get{ return _treeViewSelectedItem;}
    set {_treeViewSelectedItem = value; NotifyPropertyChanged("TreeViewSelectedItem");}
}

Treeview xaml

<TreeView ... SelectedItem={Binding TreeViewSelectedItem Mode=OneWayToSource}".../>

custom control xaml

<UserControl>
    <Control.Resources>
        <DataTemplate DataType="{x:Type Plane}">
        ....
        </DataTemplate> 
        <DataTemplate DataType="{x:Type Train}">
        ....
        </DataTemplate>
        <DataTemplate DataType="{x:Type Automobile}">
        ....
        </DataTemplate>
    </Control.Resources>

    <ContentControl Content={Binding TreeViewSelectedItem}"/>
</Usercontrol>
MarcE
  • 3,586
  • 1
  • 23
  • 27
  • Yes, I want to show the selecteditem in de detailscreen, because the tree only shows its name, the detailscreen should display more data of the selected treeviewitem. I cannot find the SelectedItem property of my treeview. – supercell Apr 21 '11 at 08:53
  • Looks like SelectedItem is read only and can't be bound - which kind of kills my answer :-( you could still accomplish the same thing by putting a handler in the tree view code behind for the SelectedItemChanged event, then poking the view model property from there.... (DataContext as ViewModelType).TreeViewSelectedItem = Treeviewcontrol.SelectedItem – MarcE Apr 21 '11 at 09:15