如何防止自动选择单选按钮 [英] How to prevent auto-selection of radio buttons

查看:61
本文介绍了如何防止自动选择单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

添加 Gtk# 实例时RadioButton 到 GUI,它作为其单选按钮组的第一个成员被自动检查(激活").

When adding an instance of the Gtk# RadioButton class to a GUI, it is automatically checked ("activated") as the first member of its radio button group.

对于Gtk,这已经在某种程度上进行了讨论在另一个问题中,重点是所选答案用户希望始终选择一个单选按钮.

For Gtk, this has been discussed to some extent in another question, with the main point of the selected answer being that users expect one radio button to be selected at all times.

我不反对这一点.

然而,我以某种方式自动生成我的用户界面,因此每个单选按钮都链接到一个数据模型,但是没有一个单选按钮可以在任何时候获得对其他任何一个的引用RadioButton 实例.数据模型确保始终选中一个单选按钮.

However, I am automatically generating my user interface in a way so each radio button is linked to a data model, but none of the radio buttons can, at any time, get a reference to any of the other RadioButton instances. The data model ensures that one radio button is checked at all times.

除了足以满足用户的期望之外,我认为这是一个很好的做法,像这样,数据完整性是由数据模型而不是 GUI 来确保的.

Beside being sufficient to match user expectations, I consider this good practice, as like this, data integrity is ensured by the data model, not by the GUI.

不幸的是,Gtk# 会像这样自动检查所有单选按钮,因为它认为每个单选按钮都是其组中的第一个.要将各种单选按钮添加到同一组中,我必须将组中的第一个单选按钮传递给其他单选按钮的构造函数-如上所述,我不能这样做,因为我没有任何方法可以获取实例化其他按钮时对第一个单选按钮的引用.

Unfortunately, Gtk# will automatically check all radio buttons like this, as it considers each radio button to be the first in its group. For adding the various radio buttons to the same group, I would have to pass the first radio button in the group to the constructor of the other radio buttons - which I cannot, as pointed out above, as I do not have any way to get the reference to that first radio button when instantiating the others.

设置 Active 将单选按钮的属性设置为 false 没有帮助,也没有调用 Toggle 方法.

Setting the Active property of the radio button to false does not help, nor does invoking the Toggle method.

有没有办法抑制这种自动选择,可能是通过子类化和覆盖我还没有找到的东西?或者,是否可以强制使用 CheckButton 看起来像用于此目的的单选按钮?

如果真的没有其他解决方案,我将尝试实施另一个答案中建议的解决方案,其中涉及添加我的每个单选按钮都有第二个隐藏的单选按钮,但我会发现这对于生产代码来说非常糟糕.

If there is really no other solution, I will try and implement the solution suggested in another answer that involves adding a second hidden radio button for each of my radio buttons, but I would find that extremely hacky for production code.

推荐答案

Josip 解决方案的扩展:创建您自己的单选按钮小部件,实际上只包含两个 gtk 单选按钮,其中一个总是隐藏的.然后你的工厂创建了这个而不是原来的 gtk 按钮......

Extension to Josip's solution: Create your own radio button widget, actually containing nothing else than two gtk radio buttons, one of which always is hidden. Your factory then creates this one instead of the original gtk button...

这篇关于如何防止自动选择单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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