Radiobuttons Ischecked属性在wpf mvvm中不起作用

Radiobuttons Ischecked属性在wpf mvvm中不起作用,wpf,mvvm,Wpf,Mvvm,我无法将两个单选按钮绑定到我的xaml表单和IsChecked属性,这些属性具有相同的组名,我还使用了nullabletoboolconverters。但是,radiobuttons ischecked属性在我的代码中没有更改(当我们在第二个radiobuttons之后点击第一个radiobutton时,它根本不会点击断点),我将分别绑定其中两个的ischecked属性,因为我需要基于radiobuttons属性设置窗体上其他面板的可见性 以下是我的xaml代码,后面是radiobutton属性

我无法将两个单选按钮绑定到我的xaml表单和IsChecked属性,这些属性具有相同的组名,我还使用了nullabletoboolconverters。但是,radiobuttons ischecked属性在我的代码中没有更改(当我们在第二个radiobuttons之后点击第一个radiobutton时,它根本不会点击断点),我将分别绑定其中两个的ischecked属性,因为我需要基于radiobuttons属性设置窗体上其他面板的可见性

以下是我的xaml代码,后面是radiobutton属性的viewmodel.cs代码:

   <RadiobuttonSelectedConverter x:Key="CheckedSelection"/>// declaring my converter class in my resource dictionary.

  <StackPanel Orientation="Horizontal" Grid.Column="0" Grid.Row="3" Margin="10,5,0,0">
        <RadioButton GroupName="RadiosGroup" IsChecked="{Binding IsRadioButton1,Mode=TwoWay,
             Converter={StaticResource CheckedSelection}, ConverterParameter=true}">
                    First</RadioButton>
        <RadioButton GroupName="RadiosGroup" 
                     Margin="40,0,0,0" IsChecked="{Binding IsRadioButton2,Mode=TwoWay, 
            Converter={StaticResource CheckedSelection}, ConverterParameter=true}">Second</RadioButton>
    </StackPanel>



    private bool _isRadioButton1;

    public bool IsRadioButton1
    {
        get
        {
            return _isRadioButton1;
        }
        set
        {
            if _isRadioButton1!= value)
            {
                _isRadioButton1= value;

                IsRadioButton2= false;
                OnPropertyChanged("IsRadioButton1");

            }
        }
    }


    private bool _isRadioButton2;

    public bool IsRadioButton2
    {
        get
        {
            return _isRadioButton2;
        }
        set
        {
            if (_isRadioButton2 != value)
            {
                _isRadioButton2 = value;

              IsRadioButton1= false;
              OnPropertyChanged("IsRadioButton2");

            }
        }
    }

请有人帮我解决我的问题,提前谢谢。

这里有几个问题

  • 你不需要转换器。将
    IsRadioButton1
    IsRadioButton2
    类型属性设置为
    bool?
    ,并且
    双向
    绑定就足够了,或者如果tri-state不适用于您,就将其保留为
    bool
  • 设置程序中的逻辑似乎不正确。在这两种情况下,您都将另一个
    单选按钮的值设置为
    false
    ,因此如果
    IsRadioButton1
    true
    ,然后您将
    IsRadioButton2
    设置为
    true
    ,则设置器将调用
    IsRadioButton=false
    ,然后该设置器将调用
    IsRadioButton2=false
    。它们最终都将是
    false
  • 如果(值)IsRadioButton2=false,您可能希望将其读取为


    编辑 实际上,我记得,
    RadioButton
    并不像
    复选框那样绑定到
    bool
    属性。我认为您可以将所有的
    单选按钮
    绑定到一个属性,并使用
    转换器
    转换器参数
    来设置属性。我会找到一个例子,并在一分钟后发布


    好的,这里有一个解决方案,使用一个派生的
    RadioButton
    类,它的行为完全与绑定有关:


    这里有一个相关的SO问题和答案:

    就我个人而言,我根本不会这样对相关的
    单选按钮进行编码。由于您想要的选择行为与
    列表框
    的选择行为相同,因此我发现简单地使用
    列表框
    是最容易的,该列表框的样式是为每个项目使用
    单选按钮

    隐藏的代码通常包含

    • 可观察采集选项
    • string selected选项
    我将在
    列表框中使用此样式:

    <Style x:Key="RadioButtonListBoxStyle" TargetType="{x:Type ListBox}">
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="KeyboardNavigation.DirectionalNavigation" Value="Cycle" />
        <Setter Property="ItemContainerStyle">
            <Setter.Value>
                <Style TargetType="{x:Type ListBoxItem}" >
                    <Setter Property="Margin" Value="2, 2, 2, 0" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <Border Background="Transparent">
                                    <RadioButton
                                        Content="{TemplateBinding ContentPresenter.Content}" VerticalAlignment="Center"
                                        IsChecked="{Binding Path=IsSelected,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}"/>
    
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </Setter.Value>
        </Setter>
    </Style>
    
    但至于你的实际问题,我可以想出三个原因,它可能表现不正确

    第一个是在中概述的:
    IsChecked1
    的setter中将
    IsChecked2
    设置为false,而
    IsChecked2
    在其setter中将
    IsChecked1
    设置为false,因此最终结果是两个值都为false

    第二个问题是您的转换器可能有问题。你说这是一个评论,它在没有转换器的情况下工作正常,所以我认为这可能是问题的一部分


    最后,我相信更改分组的
    单选按钮
    将触发旧项的
    isoption a.IsSelected=false
    ,以及新选择项的
    isoption b.IsSelected=true
    ,这两个按钮可能在某处交叉。

    您的转换器看起来很奇怪。你为什么不简单地返回
    value==param
    ?哦..谢谢你…我实际上是从链接-''使用它的,因为你的属性类型是
    bool
    ,不能是
    null
    ,你能告诉我到底哪里出了问题吗..@user1105705,首先,您为什么要使用
    转换器
    ?您的代码在没有转换器的情况下是否正常工作?是的,但是它只会触发一次更改的属性…我的意思是在第一次选择radiobutton时…不是每次。感谢您的响应…我尝试在没有将IsRadioButton 1、IsRadioButton设置为false的情况下执行…但结果仍然相同。我无法设置返回到第一个或第二个单选按钮的属性(第三次选择)…您能更清楚地解释一下解决方法吗…再次感谢。太好了..但是,我需要知道一个单选按钮的bool值,以便以相同的形式更改另一个控件的可见性..这似乎不可能与上述过程类似,例如为两个单选按钮设置单个属性..如果可能的话,请让我知道必须在给定代码中完成的过程…谢谢杰伊,你能看看这个吗?是的,再次感谢您的回复…效果很好…我创建了一个observablecollection来生成radiobutton,在myclass中,我创建了两个属性来检查所选radiobutton的Id和名称…这样我就可以根据radiobutton Id继续处理我的条件。但是…创建一个我现在只绑定两个单选按钮的收集对象,似乎有点不寻常……无论如何,这解决了我目前的问题……谢谢。@user1105705起初我也觉得很奇怪,但随着时间的推移,我已经习惯了,现在无法以任何其他方式处理这种情况。它使以后的添加变得非常容易,我发现逻辑比试图操纵多个bool值更容易遵循和维护。很高兴你成功了:)
    <Style x:Key="RadioButtonListBoxStyle" TargetType="{x:Type ListBox}">
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="KeyboardNavigation.DirectionalNavigation" Value="Cycle" />
        <Setter Property="ItemContainerStyle">
            <Setter.Value>
                <Style TargetType="{x:Type ListBoxItem}" >
                    <Setter Property="Margin" Value="2, 2, 2, 0" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <Border Background="Transparent">
                                    <RadioButton
                                        Content="{TemplateBinding ContentPresenter.Content}" VerticalAlignment="Center"
                                        IsChecked="{Binding Path=IsSelected,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}"/>
    
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </Setter.Value>
        </Setter>
    </Style>
    
    <ListBox ItemsSource="{Binding Options}"
             SelectedValue="{Binding SelectedOption}"
             Style="{StaticResource RadioButtonListBoxStyle}" />
    
    <DockPanel>
        <ListBox ItemsSource="{Binding OptionViewModels}"
                 SelectedValue="{Binding SelectedViewModel}"
                 Style="{StaticResource RadioButtonListBoxStyle}"
                 DockPanel.Dock="Left">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding DisplayName}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    
        <ContentControl Content="{Binding SelectedViewModel}" />
    </DockPanel>