locked
Enable/disable control if any radio buttons is checked RRS feed

  • Question

  • I have 4 radio buttons and I want to enable a slider only if any of the radio buttons is checked. How to accomplish that?

    <Grid Grid.ColumnSpan="2" Grid.Row="1" HorizontalAlignment="Center">
                    <RadioButton IsChecked="{Binding SelectedLetter, ConverterParameter='A', Converter={StaticResource InputLetterToBooleanConverter}, Mode=TwoWay}" 
                                 Content="A" HorizontalAlignment="Left" VerticalAlignment="Top" Width="48.5" Height="45" Style="{DynamicResource RadioButtonInputs}" FontSize="36" Padding="0" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="{DynamicResource BrushLetterA}" Cursor="Hand"/>
                    <RadioButton IsChecked="{Binding SelectedLetter, ConverterParameter='B', Converter={StaticResource InputLetterToBooleanConverter}, Mode=TwoWay}" 
                                 Content="B" HorizontalAlignment="Left" Margin="53.5,0,0,0" VerticalAlignment="Top" Width="48.5" Height="45" Style="{DynamicResource RadioButtonInputs}" FontSize="36" Padding="0" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="{DynamicResource BrushLetterB}" Cursor="Hand"/>
                    <RadioButton IsChecked="{Binding SelectedLetter, ConverterParameter='C', Converter={StaticResource InputLetterToBooleanConverter}, Mode=TwoWay}" 
                                 Content="C" HorizontalAlignment="Left" Margin="107,0,0,0" VerticalAlignment="Top" Width="48.5" Height="45" Style="{DynamicResource RadioButtonInputs}" FontSize="36" Padding="0" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="{DynamicResource BrushLetterC}" Cursor="Hand"/>
                    <RadioButton IsChecked="{Binding SelectedLetter, ConverterParameter='D', Converter={StaticResource InputLetterToBooleanConverter}, Mode=TwoWay}" 
                                 Content="D" HorizontalAlignment="Left" Margin="160.5,0,0,0" VerticalAlignment="Top" Width="48.5" Height="45" Style="{DynamicResource RadioButtonInputs}" FontSize="36" Padding="0" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="{DynamicResource BrushLetterD}" Cursor="Hand"/>
                </Grid>

    <Slider Minimum="10" Maximum="{Binding MaximumZoom}" Value="{Binding Zoom, Mode=TwoWay}"
                            Name="zoom" Margin="20.508,22.333,0,0" VerticalAlignment="Top" Style="{DynamicResource SliderHorizontalType1}"/>


    Take a look at WPF FlashMessage
    About.me

    Thursday, June 13, 2013 8:28 PM

Answers

  • You could assign each of the RadioButtons a unique name and use a MultiBinding with ElementName and a converter:

    <Grid Grid.ColumnSpan="2" Grid.Row="1" HorizontalAlignment="Center">
                <RadioButton x:Name="r1" IsChecked="{Binding SelectedLetter, ConverterParameter='A', Converter={StaticResource InputLetterToBooleanConverter}, Mode=TwoWay}" 
                                 Content="A" HorizontalAlignment="Left" VerticalAlignment="Top" Width="48.5" Height="45" Style="{DynamicResource RadioButtonInputs}" FontSize="36" Padding="0" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="{DynamicResource BrushLetterA}" Cursor="Hand"/>
                <RadioButton x:Name="r2" IsChecked="{Binding SelectedLetter, ConverterParameter='B', Converter={StaticResource InputLetterToBooleanConverter}, Mode=TwoWay}" 
                                 Content="B" HorizontalAlignment="Left" Margin="53.5,0,0,0" VerticalAlignment="Top" Width="48.5" Height="45" Style="{DynamicResource RadioButtonInputs}" FontSize="36" Padding="0" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="{DynamicResource BrushLetterB}" Cursor="Hand"/>
                <RadioButton x:Name="r3" IsChecked="{Binding SelectedLetter, ConverterParameter='C', Converter={StaticResource InputLetterToBooleanConverter}, Mode=TwoWay}" 
                                 Content="C" HorizontalAlignment="Left" Margin="107,0,0,0" VerticalAlignment="Top" Width="48.5" Height="45" Style="{DynamicResource RadioButtonInputs}" FontSize="36" Padding="0" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="{DynamicResource BrushLetterC}" Cursor="Hand"/>
                <RadioButton x:Name="r4" IsChecked="{Binding SelectedLetter, ConverterParameter='D', Converter={StaticResource InputLetterToBooleanConverter}, Mode=TwoWay}" 
                                 Content="D" HorizontalAlignment="Left" Margin="160.5,0,0,0" VerticalAlignment="Top" Width="48.5" Height="45" Style="{DynamicResource RadioButtonInputs}" FontSize="36" Padding="0" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="{DynamicResource BrushLetterD}" Cursor="Hand"/>
            
            
            </Grid>
            <Slider Minimum="10" Maximum="{Binding MaximumZoom}" Value="{Binding Zoom, Mode=TwoWay}"
                            Name="zoom" Margin="20.508,22.333,0,0">
                <Slider.IsEnabled>
                    <MultiBinding Converter="{StaticResource myConverter}">
                        <Binding ElementName="r1" Path="IsChecked"/>
                        <Binding ElementName="r2" Path="IsChecked"/>
                        <Binding ElementName="r3" Path="IsChecked"/>
                        <Binding ElementName="r4" Path="IsChecked"/>
                    </MultiBinding>
                </Slider.IsEnabled>
            </Slider>

        public class MyConverter : IMultiValueConverter
        {
            public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                foreach (object value in values)
                {
                    bool b = System.Convert.ToBoolean(value);
                    if (b)
                        return true;
                }
    
                return false;
            }
    
            public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
    

    • Marked as answer by Joba Diniz Thursday, June 13, 2013 9:38 PM
    Thursday, June 13, 2013 9:17 PM