Converter for Byte to Boolean (Checkbox)
c#, checkbox, ivalueconverter, silverlight
Solution
The implementation of your Converter is not correct. You have to swap the implementations of the `Convert` and `ConvertBack` methods.
The `Convert` method converts a value from your ViewModel to a value for the View and the `ConvertBack` method converts a value from the View to a value of the ViewModel.
So in your case with using the Converter in the Binding of the `IsChecked`-Property of the CheckBox the `Convert` method gets a Byte value and converts is to a Boolean, while the `ConvertBack` method gets a Boolean and converts it to a Byte.
public class BoolToByteConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null)
return false;
else
return System.Convert.ToBoolean(value);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value != null ? System.Convert.ToByte(value) : 0;
}
}
Problem
I have am working on Silverlight 4(Existing application) with the following requirement: Brief - Should have a search view where there is a checkbox to determine if it should return records according to 1 or 0 flag stored in a certain column - The backend part is working fine(retrieving data from a WCF service) - The problem I have is converting the Boolean Checkbox value from the front-end to the Byte value(1 or 0) sent to the WCF call that fethes the data I understand this is where a convertor would work. I did some of my own searching and found this: Silverlight Bind to inverse of boolean property value This worked brilliantly for DISPLAYING "Yes" for 1 and "No" for 0. But I am having problems with sending a checkbox True or False value which does the reverse conversion: i.e It sends to the object as follows: Checked = 1 Not Checked = 0 What I have done: - As an example, I have a table with User details as follows: ``` +----+----------+--------+ | ID | USERNAME | ACTIVE | +----+----------+--------+ | | | | | 1 | John | 1 | | | | | | 2 | Jane | 0 | | | | | | 3 | Rick | 1 | | | | | | 4 | David | 0 | +----+----------+--------+ ``` I have the following Convertor class: namespace MyApplication.Silverlight.Converters { ``` public class BoolToByteConverter : IValueConverter { public int FalseValue { get; set; } public int TrueValue { get; set; } public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value == null) return FalseValue; else return (bool)value ? TrueValue : FalseValue; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return value != null ? value.Equals(TrueValue) : false; } } ``` } My XAML Namespace declaration xmlns:converters="clr-namespace:MyApplication.Silverlight.Converters;assembly=MyApplication.Silverlight" mc:Ignorable="d" d:DesignHeight="140" d:DesignWidth="800"> Resource declaration ``` <UserControl.Resources> <converters:BoolToByteConverter x:Key="COneZero" FalseValue="0" TrueValue="1" /> </UserControl.Resources> ``` Search Portion ``` <Grid x:Name="LayoutRoot"> <StackPanel Orientation="Vertical"> <Grid x:Name="UserSearchGrid"> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="140" /> <ColumnDefinition Width="120" /> <ColumnDefinition Width="120" /> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="120" /> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <dataInput:Label x:Name="lblName" Grid.Row="0" Grid.Column="0" Content="Name:" Margin="2" /> <!--Some Other search fields--> <CheckBox x:Name="chkIncludeInActiveUsers" Grid.Row="4" Grid.Column="3" Grid.ColumnSpan="3" VerticalAlignment="Center" Content="Include InActive Users" IsChecked="{Binding SearchCriteria.ActiveStatus, Converter={StaticResource COneZero}, Mode=TwoWay}" Margin="21,4,99,4" /> </Grid> </StackPanel> </Grid> </UserControl> ``` SearchCriteria is an observable object that I use to perform the filtering If there are any namespace issues you identify in the XAML, it is a typo. Please ignore them. My main issue is with the actual convertor. Thank-you