3

我无法将两个单选按钮绑定到我的 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);
    }
}

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

4

2 回答 2

6

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

代码隐藏通常包含

  • ObservableCollection<string> 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}" />

您还可以使用其他东西而不是 aString作为集合,例如 a ChildViewModel,然后根据当前项目设置您的相关视图,这意味着您不必费心Visibility在您的任何一个相关面板中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 的回答中进行了概述:您IsChecked2在 setter 中设置为 false ,IsChecked1并在其 setter 中设置为 false,因此最终结果是两个值都是 false。IsChecked2IsChecked1

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

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

于 2012-08-27T14:20:11.643 回答
1

这里有几个问题。

  1. 你不需要转换器。制造IsRadioButton1IsRadioButton2类型的属性,bool?TwoWay Binding足够了,或者只是让它bool好像三态不适合你。
  2. 您的设置器中的逻辑似乎不正确。在这两种情况下,您都将 other 的值设置RadioButtonfalse,因此如果IsRadioButton1true然后您设置IsRadioButton2true,则 setter 将调用IsRadioButton = false,然后该 setter 将调用IsRadioButton2 = false。他们最终都会成为false.

您可能希望阅读此内容if(value) IsRadioButton2 = false;


编辑

实际上,我记得,RadioButton并不意味着像 abool那样绑定到属性CheckBox。我认为您将所有RadioButtons 绑定到一个属性,并使用 aConverter 和 aConverterParameter来设置该属性。我会找到一个例子并在一分钟内发布。


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

这是一个相关的 SO 问题和答案:MVVM:将单选按钮绑定到视图模型?

于 2012-08-27T13:25:52.333 回答