在WPF中,将IsEnabled绑定到IsChecked

8

我有一个CheckBox和RadioButton控件,我需要将IsEnabled属性绑定到IsChecked属性上。

<CheckBox x:Name="check_box" Content="CheckBox" IsChecked="True" />

<RadioButton Content="Depending Component" IsChecked="True" Margin="15,3,0,0" IsEnabled="{Binding check_box.IsChecked}" />

有没有不需要编写代码就能完成的方法?
1个回答

21

是的,您需要使用 ElementName (MSDN) 绑定:

<CheckBox x:Name="check_box" Content="CheckBox" IsChecked="True" />
<RadioButton IsEnabled="{Binding ElementName=check_box, Path=IsChecked, 
                                 TargetNullValue=false}" .../>

6
IsCheckedNullable<bool> 类型的,因此最好在绑定时设置一些 TargetNullValue,例如 False - Mike Strobel
2
@MikeStrobel 我不知道你可以指定那个;真是个很酷的功能!我已经把它加到我的答案中了,谢谢你提供这个信息。 - BradleyDotNET

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接