How to use IValueConverter to convert zeros to booleans?
I use wpf to try to display a bunch of booleans (in cells). When a new record is created, these values ​​are zero and are displayed as "undefined" in the fields. I want zeros to be displayed and stored as false values.
I tried to create a NullToBoolean converter that takes null values ​​from the database and displays them as false , and then saves them as false when the user clicks save. (Essentially, I try to prevent the user from double-clicking the checkboxes (once to make it true and then make it false). This seems to work on import, that is, null values ​​appear as false, but if I make a scoreboard with two clicks, the value does not change in the database when saved.
My converter:
[ValueConversion(typeof(bool), typeof(bool))] public class NullBooleanConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value != null) { return value; } return false; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { if (value != null) { return value; } return null; } }
One of the checkboxes with which I am trying to work with the converter:
<CheckBox Grid.Column="1" Grid.Row="0" Padding="5" Margin="5" VerticalAlignment="Center" Name="chkVarianceDescriptionProvided" IsThreeState="False"> <CheckBox.IsChecked> <Binding Path="VarianceDescriptionProvided" Mode="TwoWay"> <Binding.Converter> <utils:NullBooleanConverter /> </Binding.Converter> </Binding> </CheckBox.IsChecked> </CheckBox>
I don’t know, the problem is that my code is wrong, or if this is the case of a converter that thinks nothing has changed, so it doesn’t need ConvertBack . I tried all Mode and switched the code in Convert with ConvertBack, but nothing works.
Can someone point out what I need to do to fix this?