基于IsChecked的RadioButtons启用按钮

时间:2017-08-17 08:00:25

标签: c# wpf

我有一个带有三个单选按钮和一个按钮的简单窗口。如果选择了三个单选按钮中的任何一个,我只想启用该按钮。编码的最简单方法是什么?我找到了b2v转换器的方法,但似乎你不能一次对多个元素进行可见性布尔检查(我需要检查是否检查了三个单选按钮中的任何一个)。

截图:

enter image description here

代码:

<!-- Why No Upgrade Content-->
        <StackPanel Name="WhyNoUpgradeScreen" Visibility="Hidden">
            <TextBlock Text="Sorry You're Not Upgrading to Windows 10" HorizontalAlignment="Center" FontWeight="SemiBold"></TextBlock>
            <TextBlock Text="Please let us know why:" HorizontalAlignment="Center" Height="38" Margin="0 20 0 0" TextWrapping="Wrap"></TextBlock>
            <Grid Margin="0 0 0 0" HorizontalAlignment="Center">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>

                <RadioButton Name="NeedWin7RadioButton" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Top">
                    <RadioButton.Content>
                        <TextBlock Text="Need Windows 7 or 8 for my job." TextWrapping="Wrap"/>
                    </RadioButton.Content>
                </RadioButton>
                <RadioButton Name="CantUpgradeRadioButton" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10 0 10 0">
                    <RadioButton.Content>
                        <TextBlock Text="I've tried and can't upgrade." TextWrapping="Wrap"/>
                    </RadioButton.Content>
                </RadioButton>
                <RadioButton Name="OtherRadioButton" Grid.Column="2" HorizontalAlignment="Left" VerticalAlignment="Top">
                    <RadioButton.Content>
                        <TextBlock Text="Other." TextWrapping="Wrap"/>
                    </RadioButton.Content>
                </RadioButton>
            </Grid>
            <Grid Margin="0 30 0 0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition></ColumnDefinition>
                    <ColumnDefinition></ColumnDefinition>
                </Grid.ColumnDefinitions>

                <Button Name="BackButton_WhyNoUpgrade" Margin="30 0 150 0" Grid.Column="0" Content="Back" Click="BackButton_WhyNoUpgrade_Click" ></Button>
                <Button Name="SendFeedbackButton_WhyNoUpgrade" Margin="85 0 0 0" Grid.Column="1" Content="Send Feedback" HorizontalAlignment="Center" Click="SendFeedbackButton_WhyNoUpgrade_Click" />
            </Grid>
        </StackPanel>�

1 个答案:

答案 0 :(得分:2)

您可以为每个单选按钮创建一个触发器

这基本上说:“当检查第一个无线电时,或者当选中第二个无线电时,或者当检查第三个无线电时启用我”

<Button Name="SendFeedbackButton_WhyNoUpgrade" Margin="85 0 0 0" Grid.Column="1" Content="Send Feedback" HorizontalAlignment="Center" Click="SendFeedbackButton_WhyNoUpgrade_Click" >
  <Button.Style>
    <Style TargetType="Button">

      <Setter Property="IsEnabled" Value="False"/>

      <Style.Triggers>

        <DataTrigger Binding="{Binding Path=IsChecked, ElementName=NeedWin7RadioButton}" Value="True">
          <Setter Property="IsEnabled" Value="True"/>
        </DataTrigger>

        <DataTrigger Binding="{Binding Path=IsChecked, ElementName=CantUpgradeRadioButton}" Value="True">
          <Setter Property="IsEnabled" Value="True"/>
        </DataTrigger>

        <DataTrigger Binding="{Binding Path=IsChecked, ElementName=OtherRadioButton}" Value="True">
          <Setter Property="IsEnabled" Value="True"/>
        </DataTrigger>

      </Style.Triggers>
    </Style>
  </Button.Style>
</Button>

请注意,当您检查一个RadioButton时,将永久启用按钮,因为您无法取消选中组中的所有RadioButton(而不是通过点击)

也不是说如果你正在使用MVVM方法并且Button有一个绑定到viewModel的Command属性,那么Button.IsEnabled将基于该命令的CanExecute方法的结果