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

4

1 回答 1

0

如果树视图和详细信息都显示相同的对象(即树视图的 ItemsSource 包含您想要在自定义控件中作为数据模板的对象),那么您应该能够在两个控件共享的基础 ViewModel 上设置一个属性并让自定义控件显示与数据模板相关的内容。

例如,在 ViewModel 中:

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

树视图 xaml

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

自定义控件 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>
于 2011-04-21T08:32:15.327 回答