.net Wpf单选按钮组绑定
我在同一组中有3个单选按钮。单选按钮表示要运行的3种不同操作。 我还有.net Wpf单选按钮组绑定,.net,wpf,xaml,mvvm,.net,Wpf,Xaml,Mvvm,我在同一组中有3个单选按钮。单选按钮表示要运行的3种不同操作。 我还有按钮运行 默认情况下,所有单选按钮都有IsChecked=“false”。我需要有按钮“运行”IsEnabled=“False”,直到选中任何单选按钮。这可以防止用户在实际选择他想做的事情之前,无脑地点击“运行” 我知道如何在代码隐藏或ViewModel中实现它,但我想知道有没有办法在XAML中实现这种逻辑?只有在选中任何单选按钮时才启用按钮的任何类型的触发器?我认为最好的方法仍然是使用VM方法,对Run按钮绑定到的命令使用C
按钮运行
默认情况下,所有单选按钮都有IsChecked=“false”
。我需要有按钮
“运行”IsEnabled=“False”
,直到选中任何单选按钮。这可以防止用户在实际选择他想做的事情之前,无脑地点击“运行”
我知道如何在代码隐藏或ViewModel中实现它,但我想知道有没有办法在XAML中实现这种逻辑?只有在选中任何单选按钮时才启用按钮的任何类型的触发器?我认为最好的方法仍然是使用VM方法,对Run按钮绑定到的命令使用CanCommandExecute。您也可以使用一些纯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属性。Thx。这就是我所需要的。