单选按钮组XAML

ojs*_*sim 10 c# wpf xaml radio-button

我在XAML中有六个单选按钮,我想创建两个组.似乎WPF没有radiobutton组元素,所以我该怎么做呢?

Bit*_*KFu 22

您必须为元素指定GroupName.

 <RadioButton GroupName="Group1"/>
Run Code Online (Sandbox Code Playgroud)


Joe*_*ant 12

BitKFu建议使用该GroupName物业将有效,但有一点需要注意.如果使用组名,则组的范围RadioButton将不同.

如果您创建一个UserControl带有3的RadioButtonsall并在GroupName其中"MyRadioGroup"放置2个这样的控件Window,您会注意到所有6个RadioButtons就像它们是一个组一样.

发生这种情况是因为当RadioButton正在更新其他RadioButtons时,它通常只调整RadioButton其直接父节点的子节点DependencyObject.但是,当GroupName使用该范围时,范围会扩展到根Visual(Window例如),并且它将调整RadioButton该根下具有相同的所有s GroupName.

因此,有时最好将它们与额外的面板分开.