2013-06-03 33 views
1

我试图了解RadioButton中的IsChecked属性如何工作,因为我构建的自定义控件具有类似的行为。具有类似RadioButton IsChecked功能的自定义控件

我需要的信息是RadioButton自动将IsChecked变为false当其他RadioButton被选中。

见例如:

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

如果我在RadioButton点击与"Info 2"其它无线电自动变为选中。

但如果收音机处于diferent 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> 

Info 1.1点击了Info 2.1保持检查。

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

+0

也许你可以使用类似这样的解决方案:http://www.dragonshed.org/blog/2009/03/08/radiobuttons-in-a-datatemplate-in-silverlight /编辑:另外,你最后一个示例代码似乎是矛盾的。这听起来像你想要所有4个RadioButton被链接,但你默认为其中两个设置了“IsChecked = true”。 –

回答

0

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

+0

确定收音机组是父控制的方面。但是我不能从'ToggleButton'继承,因为在我的用户控件中有许多行为是我不想要的。 我如何从'Parent'属性获得所有其他兄弟姐妹? –

相关问题