WPF TreeView 选定项并显示用户控件 [英] WPF TreeView Selected Item and showing User Controls

查看:25
本文介绍了WPF TreeView 选定项并显示用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 TreeViews 和 User Controls 使自己陷入困境;我对 WPF 还很陌生,所以提前道歉.

I'm getting myself all in a pickle with TreeViews and User Controls; I'm fairly new to WPF so apologies in advance.

概要

我的 TreeView 项目有一组 VM 类.所以 TreeView 绑定到一个 [Parent] VM 实例的集合,每个实例都有一个 [Child]ren 的集合,每个 [Child] 都有其他数据和其他集合(我不会让你厌烦).

I have a collection of VM classes for my TreeView Items. So the TreeView is bound to a collection of [Parent] VM instances, each of which has a collection of [Child]ren, and each [Child] has other data and other collections (which I won't bore you with).

TreeView 位于表单的左侧,右侧我有一个用户控件,该控件应该仅根据所选的 TreeViewItem 类型可见.

The TreeView is on the left side of the form, and on the right I have a user control which should only be visible depending on the selected TreeViewItem type.

因此,如果 TreeView 的选定项是[Child]"类型,则用户控件应该是可见的.

So, if the selected item of the TreeView is of '[Child]' type then the User Control should be visible.

问题

我正在努力寻找如何检测何时选择了 TreeView 中的一个项目,以便我可以显示/隐藏用户控件.

I'm struggling with how to detect when an Item in the TreeView is selected so that I can show/hide the User control.

我想到但不喜欢的一种方法是将 TreeViewItem 的IsSelected"属性绑定到 [Child] VM 类,然后向主 VM 引发事件,这将通过 DP 显示/隐藏 UC.但这需要大量的事件等,对我来说,看起来很混乱.

One way I thought of, but do not like, is to bind the 'IsSelected' property of the TreeViewItem to the [Child] VM class, then raise an event up to the main VM, which would show/hide the UC via DP's. But this requires a whole load of Events etc and to me, just seems, messy.

总结

对于我的一生,我无法弄清楚如何做到这一点,我一定是累了或什么……或愚蠢……或两者兼而有之.

For the life of me I cannot work out how to do this, I must be tired or something...or stupid...or both.

我想要做的就是在 TreeView 上选择一个 Item 并且适当的 UserControl 显示 TreeViewItem 的相关数据,我可以在 Windows 窗体中很容易地做到这一点,但显然我并没有在非常 WPF 中考虑这一点'现在的方式.

All I want to do is select an Item on the TreeView and the appropriate UserControl displays with the relevant data of the TreeViewItem, which I could in Windows Forms very easily, but obviously I'm not thinking about this in a very WPF'ish way at the moment.

任何指向文章等的链接表示赞赏.

Any links to articles etc appreciated.

鼓起勇气接受任何​​回应.

Big up your chest for any responses.

推荐答案

就您而言,我更喜欢 DataTemplateSelector.它是一个对象,它提供了一种基于数据绑定对象(这里是您的 ViewModel)选择 DataTemplate 的方法.

In your case, i would prefer a DataTemplateSelector. It´s a object which provides a way to select a DataTemplate based on the databound object - here your ViewModel.

只需在您的窗口中放置一个 ContentControl 并将 Content-Property 绑定到 TreeView 的 SelectedItem-Property.将 ContentControl 的 ContentTemplateSelector-Property 设置为您自己的 DataTemplate Selector 的引用.

Just put a ContentControl in your Window and bind the Content-Property to the SelectedItem-Property of your TreeView. Set the ContentTemplateSelector-Property of the ContentControl to a reference of your own DataTemplate Selector.

DataTemplateSelector 将根据您的要求选择具有定义的 UserControl 的正确 DataTemplate.

The DataTemplateSelector will choose the correct DataTemplate with the defined UserControl by your requirements.

   <DataTemplate x:key="VMParent">
      <local:ParentUI DataContext="{Binding}" />
   </DataTemplate/>

   <DataTemplate x:key="VMChild">
      <local:ChildUI DataContext="{Binding}" />
   </DataTemplate/>

你好:)

这篇关于WPF TreeView 选定项并显示用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆