2014-04-21 102 views
0

我在同一組中有3 RadioButton。 RadioButton代表3種不同的運行。 我也有Button「運行」。Wpf RadioButton組綁定

默認情況下,所有RadioButtons都有IsChecked="false"。我需要有Button「運行」IsEnabled="False",直到任何RadioButton被檢查。這可以防止用戶在實際選擇他想要做的事情之前無腦地點擊「運行」。

我知道如何在代碼背後或在ViewModel,但我不知道有什麼辦法可以使這個邏輯在XAML?只有在任何一個RadioButton被選中的情況下才會啓用Button的任何類型的觸發器?

回答

1

我認爲最好的方法仍然是通過使用CanCommandExecute執行Run按鈕所綁定的命令來使用VM方法。你也可以使用一些純粹的XAML。對於爲例:

<Grid xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"> 

    <i:Interaction.Triggers> 
     <i:EventTrigger SourceName="rbChoice1" EventName="Checked"> 
      <i:Interaction.Behaviors> 
       <ei:ConditionBehavior> 
        <ei:ConditionalExpression ForwardChaining="Or"> 
         <ei:ComparisonCondition LeftOperand="{Binding IsChecked, ElementName=rbChoice1}" RightOperand="True"/> 
         <ei:ComparisonCondition LeftOperand="{Binding IsChecked, ElementName=rbChoice2}" RightOperand="True"/> 
         <ei:ComparisonCondition LeftOperand="{Binding IsChecked, ElementName=rbChoice3}" RightOperand="True"/> 
        </ei:ConditionalExpression> 
       </ei:ConditionBehavior> 
      </i:Interaction.Behaviors> 
      <ei:ChangePropertyAction TargetName="btnRun" PropertyName="IsEnabled" Value="True"/> 
     </i:EventTrigger> 

     <i:EventTrigger SourceName="rbChoice2" EventName="Checked"> 
      <i:Interaction.Behaviors> 
       <ei:ConditionBehavior> 
        <ei:ConditionalExpression ForwardChaining="Or"> 
         <ei:ComparisonCondition LeftOperand="{Binding IsChecked, ElementName=rbChoice1}" RightOperand="True"/> 
         <ei:ComparisonCondition LeftOperand="{Binding IsChecked, ElementName=rbChoice2}" RightOperand="True"/> 
         <ei:ComparisonCondition LeftOperand="{Binding IsChecked, ElementName=rbChoice3}" RightOperand="True"/> 
        </ei:ConditionalExpression> 
       </ei:ConditionBehavior> 
      </i:Interaction.Behaviors> 
      <ei:ChangePropertyAction TargetName="btnRun" PropertyName="IsEnabled" Value="True"/> 
     </i:EventTrigger> 

     <i:EventTrigger SourceName="rbChoice3" EventName="Checked"> 
      <i:Interaction.Behaviors> 
       <ei:ConditionBehavior> 
        <ei:ConditionalExpression ForwardChaining="Or"> 
         <ei:ComparisonCondition LeftOperand="{Binding IsChecked, ElementName=rbChoice1}" RightOperand="True"/> 
         <ei:ComparisonCondition LeftOperand="{Binding IsChecked, ElementName=rbChoice2}" RightOperand="True"/> 
         <ei:ComparisonCondition LeftOperand="{Binding IsChecked, ElementName=rbChoice3}" RightOperand="True"/> 
        </ei:ConditionalExpression> 
       </ei:ConditionBehavior> 
      </i:Interaction.Behaviors> 
      <ei:ChangePropertyAction TargetName="btnRun" PropertyName="IsEnabled" Value="True"/> 
     </i:EventTrigger> 
    </i:Interaction.Triggers> 

    <Grid HorizontalAlignment="Center" VerticalAlignment="Center" Width=" 300"> 
     <Grid.RowDefinitions> 
      <RowDefinition/> 
      <RowDefinition Height="10"/> 
      <RowDefinition/> 
      <RowDefinition Height="10"/> 
      <RowDefinition/> 
      <RowDefinition Height="10"/> 
      <RowDefinition/> 
     </Grid.RowDefinitions> 

     <RadioButton x:Name="rbChoice1" Grid.Row="0" Content="Choice 1"/> 
     <RadioButton x:Name="rbChoice2" Grid.Row="2" Content="Choice 2"/> 
     <RadioButton x:Name="rbChoice3" Grid.Row="4" Content="Choice 3"/> 

     <Button x:Name="btnRun" Grid.Row="6" Content="Run" IsEnabled="False"/> 

    </Grid> 

</Grid> 

不幸的是我:EventTrigger不採取路由事件,否則這可能只用一個EventTrigger來寫。但我認爲將其擴展爲接受路由事件應該很容易。

另一個解決方案是編寫一個MultiBindingConverter,它接收一個bools數組並對它們執行OR運算符並返回結果。通過這種方式,您可以將IsEnabled屬性綁定到無線電的所有IsChecked屬性。

+0

Thx。這就是我所需要的。 –