【问题标题】:Radiobuttons Ischecked property is not working in wpf mvvm单选按钮 Ischecked 属性在 wpf mvvm 中不起作用
【发布时间】:2012-08-21 23:05:27
【问题描述】:

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

以下是我的 xaml 代码,稍后是我的 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");

            }
        }
    }

以下是我的转换器代码:

  [ValueConversion(typeof(bool?), typeof(bool))]
public class RadiobuttonSelectedConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        bool param = bool.Parse(parameter.ToString());
        if (value == null)
        {
            return false;
        }
        else
        {
            return !((bool)value ^ param);
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        bool param = bool.Parse(parameter.ToString());
        return !((bool)value ^ param);
    }
}

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

【问题讨论】:

  • 您的转换器看起来很奇怪。为什么不直接返回value == param
  • 哦..谢谢你......我实际上是从链接中使用它 - “geekswithblogs.net/claraoscura/archive/2008/10/17/125901.aspx”,原样......你能告诉我它到底哪里出错了。 ...
  • @user1105705 既然您的属性是bool 类型,它不能是null,那么您为什么首先使用Converter?如果没有转换器,您的代码是否可以正常工作?
  • 是的,但是它只触发一次更改的属性......我的意思是第一次选择单选按钮......不是每次。

标签: wpf mvvm


【解决方案1】:

就我个人而言,我根本不会像这样编码关联的RadioButtons。由于您想要的选择行为与 ListBox 使用的相同,因此我发现最简单的方法是简单地使用 ListBox,其样式为每个项目使用 RadioButtons

代码隐藏通常包含

  • ObservableCollection&lt;string&gt; Options
  • string SelectedOption

我会将这种风格用于ListBox

<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}" />

你也可以用别的东西代替String作为集合,比如ChildViewModel,然后根据当前项设置你的相关View,这意味着你不必费心@987654333在您的ViewModel 中关联面板的@。

<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>

但至于您的实际问题,我可以想到 3 个可能行为不正确的原因。

第一个在Jay's answer 中进行了概述:您在IsChecked1 的setter 中将IsChecked2 设置为false,而IsChecked2 在其setter 中将IsChecked1 设置为false,因此最终结果是两个值都是假的。

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

最后,我相信更改分组 RadioButtons 将触发旧项目的 IsOptionA.IsSelected = false 和新选择的项目的 IsOptionB.IsSelected = true,并且这两个可能会在某个地方交叉。

【讨论】:

  • yah 再次感谢您的回复....效果很好...我创建了一个 observablecollection 来生成单选按钮,并且在 myclass 中我创建了几个属性检查所选单选按钮的 ID 和名称...以便我可以根据单选按钮 ID 继续处理我的条件。但是...创建一个集合对象以仅绑定我现在所做的两个单选按钮,这似乎有些不寻常...无论如何,这已经以某种方式解决了我现在的问题...谢谢。
  • @user1105705 一开始我也觉得很奇怪,但随着时间的推移我已经习惯了,现在无法以任何其他方式处理这种情况。它使以后添加变得非常容易,而且我发现逻辑比尝试操作多个 bool 值更容易遵循和维护。很高兴它为你解决了:)
【解决方案2】:

这里有几个问题。

  1. 您不需要转换器。将 IsRadioButton1IsRadioButton2 属性设置为 bool? 类型,TwoWay Binding 就足够了,或者如果三态不适用于您,则将其保留为 bool
  2. 您的设置器中的逻辑似乎不正确。在这两种情况下,您都将另一个RadioButton 的值设置为false,因此如果IsRadioButton1true,然后您将IsRadioButton2 设置为true,则setter 将调用IsRadioButton = false,并且那么该设置器将调用IsRadioButton2 = false。他们最终都会成为false

你可能希望这个阅读if(value) IsRadioButton2 = false;


编辑

实际上,我记得,RadioButton 并不意味着像CheckBox 那样绑定到bool 属性。我认为您将所有RadioButtons 绑定到一个属性,并使用Converter ConverterParameter 来设置属性。我会找到一个示例并在稍后发布。


好的,这是一种解决方案,使用派生的 RadioButton 类,它的行为纯粹与绑定:http://blogs.msdn.com/b/mthalman/archive/2008/09/04/wpf-data-binding-with-radiobutton.aspx

这是一个相关的 SO 问题和答案:MVVM: Binding radio buttons to a view model?

【讨论】:

  • 感谢您的回复...我尝试在不将 IsRadioButton1,IsRadioButton 设置为 false 的情况下执行...但结果仍然相同。我无法设置那些一旦回到第一个或第二个单选按钮的属性(第三次选择)..你能否更清楚地解释一下,解决它的方法..再次感谢。
  • 那太好了..但是,我需要知道一个单选按钮的布尔值来更改相同形式的另一个控件的可见性..这似乎是不可能的上述过程,如设置两个单选按钮的单个属性.....如果可以的话,请告诉我在给定代码中必须完成的过程...谢谢。
  • @Jay 你能看看这个吗? stackoverflow.com/questions/38434625/…
猜你喜欢
  • 2021-05-24
  • 2015-10-25
  • 1970-01-01
  • 2011-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多