C# WPF 中的只读复选框 [英] A read-only CheckBox in C# WPF

查看:23
本文介绍了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:

  1. CheckBox 已启用并准备好供用户单击,IsChecked 表示存储在数据结构中的绑定布尔值
  2. 用户点击 CheckBox 导致点击事件触发,但数据结构中的绑定值没有更新,CheckBox 的视觉表示没有更新,但它被禁用以停止进一步点击
  3. 点击事件触发一条消息发送到远程设备,需要一些时间来响应
  4. 远程设备响应导致数据结构更新为新值,然后绑定更新 isChecked 状态,CheckBox 重新启用以供进一步点击

我遇到的问题是,尽管 OneWay 数据绑定在单击 CheckBox 时不会更新数据结构,但视觉表示确实发生了变化(我认为这很奇怪,现在 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() 事件中的更改并在那里禁用,但这非常混乱.我还可以使用数据结构值的 set 属性来设置 isEnabled 值,该值也绑定到重新启用 CheckBox 但这似乎也很混乱.

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 类?如何停止更新视觉表示?

Is there a clean way to do this? Perhaps with a derived CheckBox class? How can I stop the visual representation getting updated?

谢谢

埃德

推荐答案

数据绑定到 IsHitTestVisible 属性?

例如,假设采用 MVVM 方法:

For example, assuming an MVVM approach:

  1. 向您的视图模型添加一个 IsReadOnly 属性,最初设置为 true 以允许点击.
  2. 将此属性绑定到 CheckBox.IsHitTestVisible.
  3. 在第一次点击后,更新您的视图模型以将此值设置为 false,以防止任何进一步的点击.
  1. Add a IsReadOnly property to your view model, initially set as true to allow click.
  2. Binding this property to CheckBox.IsHitTestVisible.
  3. After the first click, update your view model to set this value to false, preventing any further clicks.

我没有这个确切的要求,我只需要一个始终只读的复选框,它似乎很好地解决了问题.另请注意 Goran 下面关于 Focusable 属性的评论.

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屋!

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