禁用 WPF TreeView(或 TreeViewItem)选择?
有没有一种好的方法(除了重新模板化整个 TreeViewItem.Template )来禁用 TreeView 中的选择?
我基本上是在寻找 TreeView
的 ItemsControl
样式(ItemsControl
是在 ListBox< 上“禁用”选择的最佳用途) /code>,阅读 此 帖子)
Is there a nice way (except retemplating the whole TreeViewItem.Template
) to disable selection in TreeView
?
I am basically looking for the ItemsControl
style of the TreeView
(An ItemsControl
is the best use to 'disable' selection on ListBox
, read this post)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
试试这个:
Try this:
这对我来说很有效(基于 this 答案,但与项目无关 - 选择被禁用):
This did the trick for me (based on this answer, but no tied to item - selection is disabled whatsoever):
基于当前接受的答案的链接,我在我的项目中实现了这一点:
也适用于 TreeViewItem。在视图模型中:
现在您不必去打猎了!
Based off of the links to the currently accepted answer, I implemented this in my project:
Works for TreeViewItem as well. And in the view model:
Now you don't have to go hunting!
每当选择一个项目时,您都可以“取消选择”它。前任。修改 http://www.codeproject.com/KB/WPF/TreeView_SelectionWPF.aspx 中的代码 或使用 MVVM 方法(请参阅 http://www.codeproject.com/ KB/WPF/TreeViewWithViewModel.aspx) 并始终将 IsSelected 设置回 false。
Whenever an item is selected, you could "unselect" it. Ex. modify the code from http://www.codeproject.com/KB/WPF/TreeView_SelectionWPF.aspx or use a MVVM approach (see http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx) and always set IsSelected back to false.
我决定编写一个可重用的行为,HTH:
用法:
I decided to write a reusable behavior, HTH:
Usage:
我尝试过这个,它对我有用。因为我有一个简单且非动态的树视图。但我认为通过将其放在某种风格中可以发挥作用
I tried this and it worked for me. Because I have a simple and not dynamic treeview. But I think it can work by putting it in a style
我只是在选择 TreeViewItems 时取消选择它们。
我只使用 TreeView 一次。但是,如果我将其添加到多个位置,我会考虑将其添加到附加行为中。
Xaml:
背后代码:
I just unselected the TreeViewItems as they get selected.
I use TreeView only once. However if I added it in several places I would consider looking in to adding this to a Attached Behavior.
Xaml:
Code behind:
我的做法与接受的答案不同:
假设您的 ViewModel 中有一个属性(例如“ShouldPreventSelection”)
现在,当 ShouldPreventSelection 为 true 时,您希望禁用选择:
在 TreeView 中触发 PreviewSelected 事件,如下所示:
然后在代码隐藏中您可以执行以下操作:
I did this a differently than the accepted answer:
Lets say that you have a property in your ViewModel (say 'ShouldPreventSelection')
Now when ShouldPreventSelection is true you want selection to be disabled:
In your TreeView fire the PreviewSelected event like so:
Then in the codebehind you can the following: