Radio buttons group XAML

余生颓废 提交于 2019-12-23 07:46:14

问题


I have six radio buttons in XAML, and I would like to create two groups. It seems that WPF has no radiobutton group element, so how can I do this?


回答1:


You have to specify a GroupName for the element.

 <RadioButton GroupName="Group1"/>



回答2:


BitKFu's suggestion of using the GroupName property will work, but there is a caveat. If you use group names, the scope for RadioButton groups becomes different.

If you create a UserControl with 3 RadioButtons all with a GroupName of "MyRadioGroup" and place 2 such controls in your Window, you will notice that all 6 RadioButtons act like they are one group.

This happens because when the RadioButton is updating other RadioButtons it normally only adjusts RadioButtons that are children of its immediate parent DependencyObject. However, when the GroupName is used the scope expands to the root Visual (Window, for example) and it will adjust all RadioButtons under that root that have the same GroupName.

So sometimes it's better to just separate them with an extra panel.



来源:https://stackoverflow.com/questions/6056594/radio-buttons-group-xaml

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