Reputation: 1290
I have a simple window with three radio buttons and a button. I would like to only enable the button if any one of the three radio buttons has been selected. What is the simplest way to code this? I found the b2v converter methodology, but it doesn't seem like you can have your visibility boolean check done on more than one element at a time (and I need to check if any one of three radio buttons is checked).
Screenshot:
Code:
<!-- 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>�
Upvotes: 2
Views: 1574
Reputation: 35680
you can create a trigger for each radio button
this basically says: "enable me when 1st radio is checked OR when 2nd radio is checked OR when 3rd radio is checked"
<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>
note that when you check one RadioButton then button will be enabled permanently, because you cannot uncheck all RadioButtons in a group (not via clicks)
also not that if you are working using MVVM approach and Button has a Command property bound to a viewModel, then Button.IsEnabled will be based on result of CanExecute
method of that Command
Upvotes: 2