在C#WPF只读复选框 [英] A read-only CheckBox in C# WPF
问题描述
我有一个棘手的问题,我想从一个复选框微微有些不寻常的行为,似乎无法弄清楚。任何建议将是最受欢迎的。我想要的行为是:
I am having a tricky problem, I want some slightly unusual behaviour from a checkbox and can't seem to figure it out. Any suggestions would be most welcome. The behaviour I want is:
- 的复选框被启用并准备为用户点击,重器isChecked presents存储在数据结构绑定布尔值
- 用户点击复选框导致点击触发事件,但在数据结构中的绑定值不更新和CheckBox的视觉重新presentation不更新,但被禁用,停止进一步点击
- 单击事件触发消息发送到远程设备这需要一些时间来响应
- 远程设备响应造成的数据结构与新值,绑定然后更新状态器isChecked和复选框被重新启用,进一步点击
我的问题是,虽然单向数据在不更新单击该复选框,当数据结构结合的作品,视觉重presentation确实发生了改变(我认为这是奇怪的,不应该现在行动器isChecked像的指针在数据结构中的值)。
The problem I have is that although a OneWay data binding works at not updating the data structure when the CheckBox is clicked, the visual representation does change (which I think is odd, shouldn't IsChecked now act like a pointer to the value in the data structure).
我可以扭转在Click()事件的变化,做好禁用有作为但这是pretty凌乱。我也可以的数据结构值来设置它也必然要重新启用复选框的isEnabled值的设定财产,但似乎过于凌乱
I can reverse the change in the Click() event and do the disable there as well but this is pretty messy. I can also have the set property of the data structure value to set an isEnabled value which is also bound to reenable the CheckBox but that seems messy too.
有没有干净的方式做到这一点?也许派生CheckBox类?我怎样才能阻止视力重新presentation得到更新?
Is there a clean way to do this? Perhaps with a derived CheckBox class? How can I stop the visual representation getting updated?
感谢
版
推荐答案
有关数据绑定到的 IsHitTestVisible 的财产?
What about data binding to the IsHitTestVisible property?
例如,假设一个MVVM方式:
For example, assuming an MVVM approach:
- 添加的 IsReadOnly 的属性,您的视图模型,初步设置为true,以允许点击。
- 绑定这个属性来CheckBox.IsHitTestVisible。
- 第一次点击之后,更新您的视图模型这个值设置为false,preventing任何进一步的点击。
- Add a IsReadOnly property to your view model, initially set as true to allow click.
- Binding this property to CheckBox.IsHitTestVisible.
- After the first click, update your view model to set this value to false, preventing any further clicks.
我没有这个确切的要求,我只是需要一个始终只读复选框,它似乎很好地解决这个问题。还要注意以下戈兰的评论有关的可聚焦的属性。
I don't have this exact requirement, I just needed an always read only checkbox, and it seems to solve the problem nicely. Also note Goran's comment below about the Focusable property.
这篇关于在C#WPF只读复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!