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

查看:1204
本文介绍了C#WPF中的只读复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个棘手的问题,我想要一个稍微不寻常的行为,从一个复选框,似乎不能弄清楚。任何建议将是最欢迎的。我想要的行为是:


  1. 复选框已启用并准备好供用户单击,IsChecked表示存储在数据中的绑定布尔值结构

  2. 用户单击复选框导致点击事件触发,但数据结构中的绑定值未更新,并且复选框的可视化表示未更新,但已禁用停止

  3. 点击事件触发要发送到远程设备的消息,需要一些时间进行响应

  4. 远程设备响应,


  5. 问题I:如果要更新结构,请使用新值更新isChecked状态,并重新启用复选框以便进一步单击虽然OneWay数据绑定工作在不更新数据结构时,当单击CheckBox时,视觉表示确实改变(我认为是奇怪的,IsChecked现在应该像指向数据结构中的值的指针) 。



    我可以逆转Click()事件中的更改,并在那里禁用,但这是相当混乱。我也可以有数据结构值的set属性设置一个isEnabled值,也绑定到重新启用复选框,但是似乎也很乱。



    有干净的方法做到这一点?也许有一个派生的CheckBox类?如何停止更新视觉表示?



    感谢



    Ed

    解决方案

    如何将数据绑定到 IsHitTestVisible 属性?



    例如,假设使用MVVM方法:



    1. 将此属性绑定到CheckBox.IsHitTestVisible。

    2. 之后,您可以将IsReadOnly属性设置为您的视图模型第一次点击,更新您的视图模型,将此值设置为false,阻止任何进一步的点击。

    需求,我只需要一个总是只读复选框,它似乎解决问题很好。另请注意Goran在下面有关可专属性属性的评论。


    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. The CheckBox is enabled and ready for the user to click, IsChecked represents a bound boolean value stored in a data structure
    2. The user clicks the CheckBox causing the click event to fire but the bound value in the data structure is NOT updated and the visual representation of the CheckBox is NOT updated but it is disabled to stop further clicking
    3. The click event triggers a message to be sent to a remote device which takes some time to respond
    4. The remote device responds causing the data structure to be updated with the new value, the binding then updates the isChecked status and the CheckBox gets reenabled for further clicking

    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).

    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.

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

    Thanks

    Ed

    解决方案

    What about data binding to the IsHitTestVisible property?

    For example, assuming an MVVM approach:

    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.

    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天全站免登陆