Null To Boolean IValueConverter not working

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?

+6
source share
3 answers

The real problem is that you are not initializing your data objects in the first place. Do not “correct,” do it right to begin with; builders are good (for example). You should also create ViewModels / DataModels, and not directly work with your models (database, etc.).

 public class MyObjectBuilder { Checked _checked; public MyObjectBuilder() { Reset() } private void Reset() { _checked = new Checked(true); //etc } public MyObjectBuilder WithChecked(bool checked) { _checked = new Checked(checked); } public MyObject Build() { var built = new MyObject(){Checked = _checked;} Reset(); return built; } } 

then always initialize using the builder

 myObjects.Add(new MyObjectBuilder().Build()); 

or

 myObjects.Add(_injectedBuilder.Build()); // Initialises Checked to default myObjects.Add(_injectedBuilder.WithChecked(true).Build()); //True 

While this does not fix your problem, it will fix your underlying problem the way you can Unit Test. those. you can verify that the values ​​added to the list of objects are always initialized.

+1
source

Hmm, why use a converter if you can do it out of the box?

 <CheckBox IsChecked="{Binding VarianceDescriptionProvided, TargetNullValue=False}" /> 

For more information, pls look here .

+13
source

Just adjust your data before performing data binding. This is the only option. The converter will only work if the checkbox is set to "unverified" and updates your data only when interacting with the control. For instance:

 foreach (var item in items) { if (item.VarianceDescriptionProvided == null) item.VarianceDescriptionProvided = false; } 
0
source

All Articles