C# 如何将所有RadioButton IsChecked属性设置为false

C# 如何将所有RadioButton IsChecked属性设置为false,c#,wpf,radio-button,ischecked,C#,Wpf,Radio Button,Ischecked,我在一个UniformGrid中有4个单选按钮 最初,它们都设置为false 但一旦我点击其中一个,一个始终保持真实 我们的要求是,它们都可以设置为false 我试过这样的方法 <MultiDataTrigger> <MultiDataTrigger.Conditions> <Condition Binding="{Binding IsChecked, RelativeSource={RelativeSo

我在一个
UniformGrid
中有4个
单选按钮

最初,它们都设置为
false

但一旦我点击其中一个,一个始终保持真实

我们的要求是,它们都可以设置为false

我试过这样的方法

<MultiDataTrigger>
    <MultiDataTrigger.Conditions>
        <Condition Binding="{Binding IsChecked,
                   RelativeSource={RelativeSource Self}}" Value="True"/>
        <Condition Binding="{Binding IsPressed,
                   RelativeSource={RelativeSource Self}}" Value="True"/>
    </MultiDataTrigger.Conditions>
    <Setter Property="IsChecked" Value="False"/>
</MultiDataTrigger>

但它不起作用。我知道我可以使用
切换按钮
,但一次只能检查一个
单选按钮


这可能吗?

我可以马上告诉您,样式无法解决您的问题,因为一旦用户设置了DependencyProperty的值,它就会忽略样式设置器,您应该仔细阅读

另一方面,当单选按钮被选中=True时,您可以尝试自己处理鼠标单击,如下所示:

        <RadioButton GroupName="AAA" PreviewMouseLeftButtonDown="RadioButton_PreviewMouseLeftButtonDown"/>

我想这就可以了。

只有当鼠标按钮实际按下时,您的解决方案才会起作用(试着按住它,您就会看到)。 想到的最简单的方法是稍微更改
RadioButton
,以满足我们的需要:

public class UncheckableRadioButton : RadioButton
{
   protected override void OnToggle()
   {
      if (IsChecked == true)
         IsChecked = false;
      else base.OnToggle();
   }
}
如果再次单击,这些自定义收音机将取消选中。

根据要求,我将发布我的解决方案:

我只是为我的
单选按钮创建了一个事件处理程序,在这里我比较类中定义的某些值:

private int PopupID;
private bool IsPopupActive;
private void RadioButton_Click(object sender, RoutedEventArgs e)
{
    RadioButton rb = (RadioButton)sender;
    string s = (string)rb.Content;
    int id = int.Parse(s);

    if (PopupID == id && IsPopupActive == true)
    {
        foreach (RadioButton rbb in PopupGrid.Children.OfType<RadioButton>())
            rbb.SetValue(RadioButton.IsCheckedProperty, false);

        IsPopupActive = false;
    }
    else if (PopupID != id || IsPopupActive == false)
    {
        foreach (RadioButton rbb in PopupGrid.Children.OfType<RadioButton>()
                .Where(x => x != rb))
            rbb.SetValue(RadioButton.IsCheckedProperty, false);

        IsPopupActive = true;
        PopupID = id;
    }
}
private int-PopupID;
私人住宅是人口密集型的;
专用void单选按钮单击(对象发送者,路由目标e)
{
RadioButton rb=(RadioButton)发送器;
字符串s=(字符串)rb.Content;
int id=int.Parse;
if(PopupID==id&&ispopuppative==true)
{
foreach(PopupGrid.Children.OfType()中的RadioButton rbb)
rbb.SetValue(RadioButton.IsCheckedProperty,false);
ispopupacative=false;
}
else if(PopupID!=id | | ispopuppative==false)
{
foreach(PopupGrid.Children.OfType()中的RadioButton rbb)
.其中(x=>x!=rb))
rbb.SetValue(RadioButton.IsCheckedProperty,false);
ispopupacative=true;
PopupID=id;
}
}

不太喜欢这种代码隐藏解决方案,但它可以工作。

如果用键盘切换收音机怎么办?=)我想这也需要用户预览键,在检查按下的键是否是空格键时使用相同的代码…@DeMama,您是否介意分享您的解决方案(通过编辑或单独回答)?它可能对其他人很有用,对我们来说也很有趣,我们回答了这个问题=)给你。但是,请注意,这个解决方案非常适合我的需要。
private int PopupID;
private bool IsPopupActive;
private void RadioButton_Click(object sender, RoutedEventArgs e)
{
    RadioButton rb = (RadioButton)sender;
    string s = (string)rb.Content;
    int id = int.Parse(s);

    if (PopupID == id && IsPopupActive == true)
    {
        foreach (RadioButton rbb in PopupGrid.Children.OfType<RadioButton>())
            rbb.SetValue(RadioButton.IsCheckedProperty, false);

        IsPopupActive = false;
    }
    else if (PopupID != id || IsPopupActive == false)
    {
        foreach (RadioButton rbb in PopupGrid.Children.OfType<RadioButton>()
                .Where(x => x != rb))
            rbb.SetValue(RadioButton.IsCheckedProperty, false);

        IsPopupActive = true;
        PopupID = id;
    }
}