将 RadioButtons 绑定到单个属性? [英] Bind RadioButtons to single property?

查看:28
本文介绍了将 RadioButtons 绑定到单个属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 我的 RadioButton 很少,我不想绑定IsChecked"他们每个人的财产到代码中的唯一属性.
  • 我想要一个像CurrentSelected"这样的属性,并且根据以设置IsChecked".
  • 此外,我不想使用转换器.
  • 我尝试使用行为ChangePropertyAction",但它看起来像它只以一种方式工作.这是我的代码:

  • I'm having few RadioButtons and I don't want to bind "IsChecked" property of each one of them to unique property in the code.
  • I want to have a single property like "CurrentSelected" and according to that to set the "IsChecked".
  • In addition I don't want to use converters.
  • I tried to use the behavior "ChangePropertyAction" but it looks like it's working only in one way. here is my code:

<RadioButton
    x:Name="UpRadioButton"
    Margin="5"
    Content="Up"
    >
    <i:Interaction.Triggers>
        <ei:DataTrigger Binding="{Binding IsChecked, ElementName=UpRadioButton}" Value="True">
            <ei:ChangePropertyAction TargetObject="{Binding Mode=OneWay}" PropertyName="SelectedDirection" Value="{x:Static Enums:DirectionEnum.Up}" />
        </ei:DataTrigger>
    </i:Interaction.Triggers>
</RadioButton>

<RadioButton
    x:Name="DownRadioButton"
    Margin="5"
    Content="Down"
    >
    <i:Interaction.Triggers>
        <ei:DataTrigger Binding="{Binding IsChecked, ElementName=DownRadioButton}" Value="True">
            <ei:ChangePropertyAction TargetObject="{Binding Mode=OneWay}" PropertyName="SelectedDirection" Value="{x:Static Enums:DirectionEnum.Down}" />
        </ei:DataTrigger>
    </i:Interaction.Triggers>
</RadioButton>

<RadioButton
    x:Name="LeftRadioButton"
    Margin="5"
    Content="Left"
    >
    <i:Interaction.Triggers>
        <ei:DataTrigger Binding="{Binding IsChecked, ElementName=LeftRadioButton}" Value="True">
            <ei:ChangePropertyAction TargetObject="{Binding Mode=OneWay}" PropertyName="SelectedDirection" Value="{x:Static Enums:DirectionEnum.Left}" />
        </ei:DataTrigger>
    </i:Interaction.Triggers>
</RadioButton>

<RadioButton
    x:Name="RightRadioButton"
    Margin="5"
    Content="Right"
    >
    <i:Interaction.Triggers>
        <ei:DataTrigger Binding="{Binding IsChecked, ElementName=RightRadioButton}" Value="True">
            <ei:ChangePropertyAction TargetObject="{Binding Mode=OneWay}" PropertyName="SelectedDirection" Value="{x:Static Enums:DirectionEnum.Right}" />
        </ei:DataTrigger>
    </i:Interaction.Triggers>
</RadioButton>               

我的视图模型非常简单:MainViewModel.cs

my view model is very simple: MainViewModel.cs

public class MainViewModel : ViewModelBase
{ 
    private DirectionEnum _selectedDirection;

    public DirectionEnum SelectedDirection
    {
        get { return _selectedDirection; }
        set
        {
            if (_selectedDirection != value)
            {
                _selectedDirection = value;
                RaisePropertyChanged();
            }
        }
    }
    public MainViewModel()
    {     
        SelectedDirection = DirectionEnum.Up;            
    }  
}

正如您从代码中看到的,应该已经选中了向上"RadioButton...我错过了什么?

as you can see from the code, the "Up" RadioButton should be already checked... What am I missing ?

推荐答案

对我来说一个常见的解决方案是使用包含选择行为的 ListBox,并覆盖模板以将项目绘制为 RadioButtons.

A common solution for me is to use a ListBox, which contains Selection behavior, and overwrite the Template to draw items as RadioButtons instead.

我的 XAML 模板通常如下所示:

My XAML template usually looks something like this :

<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 Directions}"
         SelectedValue="{Binding SelectedDirection}"
         Style="{StaticResource RadioButtonListBoxStyle}" />

我发现管理分组 RadioButton 的选择行为比在我的代码隐藏中维护大量 IsChecked 属性或使用转换器要干净得多.

I find this much cleaner for managing selection behavior of grouped RadioButtons than maintaining a lot of IsChecked properties in my code-behind, or using converters.

这篇关于将 RadioButtons 绑定到单个属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆