2015-02-17 50 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