2015-02-17 51 views
2
  • 我有几个单选按钮,我不想要约束他们每个人的 财产“器isChecked”代码中的独特属性。
  • 我想要有一个像“CurrentSelected”和 这样的单一属性来设置“IsChecked”。
  • 另外我不想使用转换器。
  • 我试图使用行为“ChangePropertyAction”,但它看起来像 它只能以一种方式工作。这里是我的代码:单选按钮与行为结合到单一的财产

    <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

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应该已经检查... 我错过了什么?

+1

它可能只有一种方法,因为你的绑定只有一种方法......另外,转换器有什么问题吗?有一个是*很好*为此 – BradleyDotNET 2015-02-17 21:25:22

+0

转换器需要维护... – kaycee 2015-02-17 21:37:43

+0

因此,一般的代码...它的您的选择,但说你不喜欢转换器,因为他们需要“维护”是一个非常糟糕的原因/在我的书中借口。 – BradleyDotNET 2015-02-17 21:39:14

回答

7

对我来说,一个常见的解决方案是使用一个ListBox,它包含Selection行为,并覆盖Template以将项目绘制为RadioButton。

我的XAML模板通常看起来是这样的:

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

我觉得这么多的用于管理分组单选按钮的选择行为比维护会更加清晰我的代码隐藏的IsChecked属性,或使用转换器。

+0

蕾切尔,你的解决方案不仅仅是完美。谢谢 – kaycee 2015-02-17 21:35:44