How to prevent auto-selection of radio buttons

馋奶兔 提交于 2019-12-23 09:26:16

问题


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.

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.

I do not dispute that.

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.

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.

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.

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

Is there any way to suppress this automatical selection, possibly by subclassing and overriding something I could not find yet? Alternatively, is it somehow possible to force a CheckButton to look like a radio button for this purpose?

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.


回答1:


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




回答2:


Radio buttons are similar to check buttons except they are grouped so that only one may be selected/depressed at a time.

They require a group to perform their duty properly.

Try the solution from the down, it's not so hacky, just try to add one hidden radio button that is always selected.



来源:https://stackoverflow.com/questions/32386542/how-to-prevent-auto-selection-of-radio-buttons

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!