将所选方法添加到WPF中的用户控件 [英] Add selected method to user control in WPF

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

问题描述

我有一个用于TreeView的自定义WPF用户控件。我想知道如何添加类似于TreeVieeItem的Selected方法。我的最终目标是当其enabled属性设置为false时保持用户控件不被选中。

I have a custom WPF user control that is intended for use in a TreeView. I would like to know how I can add a Selected method similar to that of the TreeVieeItem. My end goal it to keep the user control from being selected when its enabled property is set to false.

推荐答案

你不能,为什么原因很简单:没有这样的方法为在类中选择> System.Windows.Controls.TreeViewItem

http://msdn.microsoft.com/en- us / library / system.windows.controls.treeviewitem%28v = vs.110%29.aspx [ ^ ]。



而且,这种方法不能出现在任何懂英语和理解合理命名约定的人的代码中。但是有一个这个名字的事件。整个问题尚不清楚。选择是 TreeView 的属性,而不是你的控件。



通常,可以选择任何树节点。使一些节点无法选择可能是一项相当困难的工作。您宁愿需要创建树视图的派生类。您需要处理许多事件,这些事件将检测尝试选择某个节点,确定是否可以选择它,如果不是,则应选择一些附近的节点。比如,如果您通过向上和向下箭头键选择节点,则选择应跳过不可选择的节点。如果你向上移动,并且顶部的节点是不可选择的,你需要取消事件及其进一步的传播,同样的事情是向下移动。这样的事情应该发生在点击不可选择的节点本身上。这将是一项很多研究和开发。没什么不可能的。 :-)



-SA
You cannot, by why simple reason: there is no such method as Selected in the class System.Windows.Controls.TreeViewItem:
http://msdn.microsoft.com/en-us/library/system.windows.controls.treeviewitem%28v=vs.110%29.aspx[^].

Moreover, such method could not appear in the code of anyone knowing English and understanding reasonable naming conventions. There is an event with this name though. The whole problem is unclear. Selection is the property of the TreeView, not your control.

Normally, any tree node can be selected. Making some nodes unselectable can be a pretty difficult work. You would rather need to create a derived class of the tree view. You would need to handle a number of events which would detect an attempt to select some node, determine if it can be selected or not, and if it is not, some nearby node should be selected instead. Say, if you select nodes by up and down arrow keys, selection should "jump" over the unselectable node. If you are moving up, and the node on top is unselectable one, you would need to cancel the event and its further propagation, same thing about moving down. Something like that should happen on the click on the unselectable node itself. This would be a good deal of research and development. Nothing impossible though. :-)

—SA


这篇关于将所选方法添加到WPF中的用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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