WP7dev

Windows Phone 7 development tips

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>


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: