开发者

Switch View based on selected TreeViewItem

开发者 https://www.devze.com 2023-02-27 06:57 出处:网络
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 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 de开发者_运维技巧tail 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


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消