使用命令绑定选择WPF Xaml TreeViewITem [英] WPF Xaml TreeViewITem selection using command Binding

查看:109
本文介绍了使用命令绑定选择WPF Xaml TreeViewITem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<TreeView Name="optionsTreeView">
                <TreeViewItem Header="View1"></TreeViewItem>
                <TreeViewItem Header="View2"></TreeViewItem>
                <TreeViewItem Header="View3"></TreeViewItem>
</TreeView>

in the above tree nodes i am trying to show respective windows..

How shall i bind the TreeviewItem Command to open a window..
for example On selection of first treenodeitem "View1" how can i open notepad 

推荐答案

这种设计存在一个基本问题,因为用户期望单击树视图以展开节点.现在,我了解到这些是您要单击的端节点,但就目前而言,树视图旨在在单击时进行扩展.

因此,如何使它的行为有所不同-并以MVVM风格,无漏洞,捕捉事件的方式进行操作?好吧,诀窍是您要单击此处标题中的文本.一定要是文字吗?好吧,不,不是-您可以更改它.但是,您将其更改为什么?好吧,从视觉上讲,您想向用户指示单击文本除了扩展节点以外还具有其他作用,并且为了使您自己更轻松,您需要带有Command的内容,您可以将其挂接到命令中.您的第一个想法可能是使用和重新设置按钮的样式,但这有点漫长.另外,如果我愿意,可以使用Hyperlink.

因此,请尝试以下操作:
There is a fundamental problem with this design as it stands because users expect clicking the treeview to expand nodes. Now, I appreciate that these are end nodes that you want to click on but, as it stands, the treeview is designed to expand on click.

So, how do you make it behave differently - and do it in a MVVM style, none-hacky, catching events way? Well, the trick is that you are clicking on the text in the header here. Does it have to be text though? Well, no it doesn''t - you can change it. But, what do you change it to? Well, visually you want to indicate to the user that clicking on the text has some effect other than expanding a node, and to make things easy for yourself you want something with a Command that you can hook your command into. Your first thought might be to use and restyle a button, but this is a bit longwinded. Alternatively, and the one I''d go with, you could use a Hyperlink.

So, try this instead:
<TreeViewItem>
  <TreeViewItem.Header>
    <TextBlock>
      <Hyperlink Command="TriggerNotepadCommand">
        Notepad
      <Hyperlink>
    </TextBlock>
  </TreeViewItem.Header>
<TreeViewItem>

然后,您要做的就是在ViewModel中连接命令代码.

Then, all you need to do is hook up the command code in your ViewModel.


这篇关于使用命令绑定选择WPF Xaml TreeViewITem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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