WP7dev

Windows Phone 7 development tips

Tag Archives: Boolean inverter

Boolean Inverter Converter

Basic value converter, but very useful when using linked CheckBox or RadioButton :


    public class BoolInverterConverter : IValueConverter

    {

        public object Convert(object value, Type targetType, object parameter,

                              System.Globalization.CultureInfo culture)

        {

            return !(bool)value;

        }

 

        public object ConvertBack(object value, Type targetType, object parameter,

                                  System.Globalization.CultureInfo culture)

        {

            return !(bool)value;

        }

    }


Example usage : 2 Checkboxes linked to the same value, one is inverted (Value “No”). This way, when checking one box, the other gets unchecked.


<StackPanel Orientation=”Horizontal” VerticalAlignment=”Top”>

<CheckBox Style=”{StaticResource RadioButtonStyle}”

                 IsChecked=”{Binding Tested, Mode=TwoWay}”>

             <TextBlock Margin=”10,0,0,0″ FontWeight=”Bold” FontSize=”24″

Text=”{Binding StringLibrary.Tested_Yes, Source={StaticResource LocalizedStrings}}”/>

       </CheckBox>

       <CheckBox Style=”{StaticResource RadioButtonStyle}”

                 IsChecked=”{Binding Tested, Mode=TwoWay,

                            Converter={StaticResource BoolInverterConverter}}”>

             <TextBlock Margin=”10,0,0,0″ FontWeight=”Bold” FontSize=”24″

Text=”{Binding StringLibrary.Tested_No, Source={StaticResource LocalizedStrings}}”/>

       </CheckBox>

</StackPanel>