具有类似RadioButton IsChecked属性功能的自定义控件

时间:2022-04-25 19:17:46

I'm trying to understand how the IsChecked property in the RadioButton works, because I'm building a custom control with have a similar behavior.

我试图理解RadioButton中的IsChecked属性是如何工作的,因为我正在构建一个具有类似行为的自定义控件。

The information I need is how a RadioButton automatically turns IsChecked to false when other RadioButton is checked.

我需要的信息是当检查其他RadioButton时,RadioButton如何自动将IsChecked转为false。

See the example:

看例子:

<StackPanel>
    <RadioButton Content="Info 1" IsChecked="True" />
    <RadioButton Content="Info 2" />
</StackPanel>

If i click in RadioButton with "Info 2" the other radio will automatically turn to unchecked.

如果我用“信​​息2”点击RadioButton,其他收音机将自动转为未选中。

But if the radios is in a diferent StackPanel like this:

但是,如果无线电在不同的StackPanel中,如下所示:

<StackPanel>
    <StackPanel>
        <RadioButton Content="Info 1.1" IsChecked="True" />
        <RadioButton Content="Info 1.2" />
    </StackPanel>
    <StackPanel>
        <RadioButton Content="Info 2.1" IsChecked="True" />
        <RadioButton Content="Info 2.2" />
    </StackPanel>
</StackPanel>

When the Info 1.1 is clicked the Info 2.1 remains checked.

单击Info 1.1时,Info 2.1仍保持选中状态。

My custom control need this behavior. How is the cleanest way to do that?

我的自定义控件需要此行为。最干净的方法是如何做到的?

1 个解决方案

#1


0  

The RadioButton is a control that is usually used as an item in a group of RadioButton controls. RadioButton controls inherit from the ToggleButton class, which in turn inherits from ButtonBase. Because RadioButton controls inherit from ToggleButton, they enable a user to change selections. When RadioButton elements are grouped, the buttons are mutually exclusive. A user can select only one item at a time within a RadioButton group. In your example radio buttons having content Info 1.1 and Info 1.2 share the same parent control hence they are grouped which means only 1 radio button is checked at a time while radio buttons having content Info 1.1 and Info 2.1 does not share the same parent control which means they are not grouped so when Info 1.1 is checked it does not make any effect on Info2.1

RadioButton是一个控件,通常用作一组RadioButton控件中的项。 RadioButton控件继承自ToggleButton类,而ToggleButton类继承自ButtonBase。因为RadioButton控件继承自ToggleButton,所以它们允许用户更改选择。当RadioButton元素被分组时,按钮是互斥的。用户可以在RadioButton组中一次只选择一个项目。在您的示例中,具有内容Info 1.1和Info 1.2的单选按钮共享相同的父控件,因此它们被分组,这意味着一次仅检查1个单选按钮,而具有内容Info 1.1和Info 2.1的单选按钮不共享相同的父控件。意味着它们没有分组,因此当选中Info 1.1时它不会对Info2.1产生任何影响

#1


0  

The RadioButton is a control that is usually used as an item in a group of RadioButton controls. RadioButton controls inherit from the ToggleButton class, which in turn inherits from ButtonBase. Because RadioButton controls inherit from ToggleButton, they enable a user to change selections. When RadioButton elements are grouped, the buttons are mutually exclusive. A user can select only one item at a time within a RadioButton group. In your example radio buttons having content Info 1.1 and Info 1.2 share the same parent control hence they are grouped which means only 1 radio button is checked at a time while radio buttons having content Info 1.1 and Info 2.1 does not share the same parent control which means they are not grouped so when Info 1.1 is checked it does not make any effect on Info2.1

RadioButton是一个控件,通常用作一组RadioButton控件中的项。 RadioButton控件继承自ToggleButton类,而ToggleButton类继承自ButtonBase。因为RadioButton控件继承自ToggleButton,所以它们允许用户更改选择。当RadioButton元素被分组时,按钮是互斥的。用户可以在RadioButton组中一次只选择一个项目。在您的示例中,具有内容Info 1.1和Info 1.2的单选按钮共享相同的父控件,因此它们被分组,这意味着一次仅检查1个单选按钮,而具有内容Info 1.1和Info 2.1的单选按钮不共享相同的父控件。意味着它们没有分组,因此当选中Info 1.1时它不会对Info2.1产生任何影响