Wpf RadioButton组绑定

时间:2014-04-21 14:05:05

标签: .net wpf xaml mvvm

我在同一组中有3 RadioButton。 RadioButtons表示要运行的3种不同操作。 我还有Button“跑”。

默认情况下,所有RadioButtons都有IsChecked="false"。我需要Button“运行”IsEnabled="False",直到检查任何RadioButtons。这样就可以防止用户在实际选择他想做的事情之前单击“运行”。

我知道如何在后面的代码或ViewModel中创建它,但我想知道有没有办法在XAML中创建这个逻辑?任何类型的触发器只有在选中任何RadioButton时才启用Button?

1 个答案:

答案 0 :(得分: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,它接受一个bool数组并对它们执行OR运算符并返回结果。通过这种方式,您可以将IsEnabled属性绑定到无线电的所有IsChecked属性。

相关问题