繁体   English   中英

WPF RadioButton组绑定

[英]Wpf RadioButton group binding

我在同一组中有3个RadioButton 单选按钮表示3种不同的操作要运行。 我也有Button “运行”。

默认情况下,所有RadioButton都具有IsChecked="false" 我需要Button “ Run” IsEnabled="False"直到选中任何RadioButtons为止。 这样可以防止用户在实际选择自己想做的事情之前无脑地单击“运行”。

我知道如何在背后的代码或ViewModel中进行编码,但是我想知道是否有任何方法可以在XAML中进行这种逻辑处理? 是否只有在选中任何RadioButtons的情况下才能使Button启用的触发器?

我认为最好的方法仍然是使用CanCommandExecute作为“运行”按钮绑定到的命令,从而寻求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>

不幸的是,i:EventTrigger不会接受路由事件,否则只能使用一个EventTrigger编写该事件。 但是我认为将其扩展为接受路由事件应该很容易。

另一种解决方案是编写一个MultiBindingConverter,它接受一个布尔数组并对其执行OR运算符并返回结果。 这样,您可以将IsEnabled属性绑定到无线电的所有IsChecked属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM