XAML 只读组合框
要在 XAML (WPF) 中设置只读 ComboBox
,您必须设置一个 ComboBox
和一个 TextBox
,根据ViewModel
上必须存在的一对属性 IsReadOnly
/IsEditable
。请注意,在此示例中,“UserNVL
”必须存在于资源中,并且它应该是一个 NameValueList
集合,允许我们将 ID
转换为名称。在本例中,RecipientID
是用户名的键。另请注意,VisibilityConverter
也必须存在于资源中,并且它是标准的 BooleanToVisibilityConverter
。
天哪!这太难找到了,我必须自己做。这允许用户选择文本框的内容。禁用的 ComboBox
绝不允许您执行此操作。
To set up a ReadOnly ComboBox
in XAML (WPF), you have to set up a ComboBox
and a TextBox
showing only one of them according to a pair of properties IsReadOnly
/IsEditable
that must exist on your ViewModel
. Note that on this sample "UserNVL
" must exist in the resources and it should be a NameValueList
collection that allows us to convert ID
to names. In this case the RecipientID
is the key for a user name. Note also the VisibilityConverter
must also exist in the resources and it's a standard BooleanToVisibilityConverter
.
Gosh! This was so hard to find I had to made it myself. This allows the user the select the content of the text box. No way a disabled ComboBox
would ever allow you to do it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
有两个名为 IsHitTestVisible 和 IsHitTestVisible 的属性。 IsTabVisible。前者使控件对鼠标事件充耳不闻,后者对键盘事件充耳不闻。
这可以帮助您,因为它不会给您的组合框提供禁用的外观,但您将成功制作只读组合框。
来源 :-
http://www.telerik.com /community/forums/wpf/combobox/isreadonly-does-sem-to-work.aspx
There are two properties named IsHitTestVisible & IsTabVisible. the former makes the control deaf to mouse events and the latter to keyboard events.
This could help you as it would not give the disabled look to your combo box but you will succeed in making a read only combo box..
Source :-
http://www.telerik.com/community/forums/wpf/combobox/isreadonly-does-seem-to-work.aspx
为什么不直接设置 IsEnabled=false 呢?
Why not just set IsEnabled=false?
我认为您会发现以这种非常简单的方式创建一个类来扩展 ComboBox 类更加容易和实用:
这样您只需将 ComboBox.IsReadOnly 属性设置为 True 即可。无需到处编写大型 XAML...
I think that you will find it much easier and practical to create a class to extend the ComboBox class in this very simple manner:
That way you just have to set ComboBox.IsReadOnly property to True. No big XAML to write everywhere...
下面是一个自定义
ComboBox
子类,它提供了我的场景所需的只读行为:关于此方法的要点:
Here is a custom
ComboBox
subclass that gives the read only behaviour I needed for my scenario:Points about this approach:
如果 IsEnabled 设置为 false,则 Combobox 值几乎不可读。我发现合适的解决方案是:
If IsEnabled is set to false, Combobox value is nearly not readable. What I found as suitable solution is: