单选按钮组 XAML
我在 XAML 中有六个单选按钮,我想创建两个组。 WPF 似乎没有单选按钮组元素,那么我该怎么做呢?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须为元素指定一个 GroupName。
You have to specify a GroupName for the element.
BitKFu 使用
GroupName
属性的建议是可行的,但有一个警告。如果您使用组名称,RadioButton
组的范围会变得不同。如果您创建一个包含 3 个
RadioButtons
的UserControl
,所有GroupName
均为“MyRadioGroup”
并将 2 个此类控件放置在在您的Window
中,您会注意到所有 6 个RadioButton
的行为就像它们是一组一样。发生这种情况是因为当
RadioButton
更新其他RadioButton
时,它通常只调整作为其直接父级DependencyObject< 的子级
RadioButton
/代码>。但是,当使用GroupName
时,范围会扩展到根Visual
(例如Window
),并且它将调整所有RadioButton
在该根下具有相同的GroupName
。因此,有时最好用额外的面板将它们分开。
BitKFu's suggestion of using the
GroupName
property will work, but there is a caveat. If you use group names, the scope forRadioButton
groups becomes different.If you create a
UserControl
with 3RadioButtons
all with aGroupName
of"MyRadioGroup"
and place 2 such controls in yourWindow
, you will notice that all 6RadioButton
s act like they are one group.This happens because when the
RadioButton
is updating otherRadioButton
s it normally only adjustsRadioButton
s that are children of its immediate parentDependencyObject
. However, when theGroupName
is used the scope expands to the rootVisual
(Window
, for example) and it will adjust allRadioButton
s under that root that have the sameGroupName
.So sometimes it's better to just separate them with an extra panel.