Search code examples
c#wpfdatagridtextcolumn

DatagridTextColumn with decimal value, allow no entry


Using .Net 4.6.
I have a DataGridTextColumn bound to a decimal? property. (I needed help from this link to get it to work at all.)
Is it possible to allow the user to enter no value? That is, the user would select the cell and delete whatever is in it.
In our application, when the user leaves the cell after deleting the content the bound property is not updated.
Thanks for any insight --

Update:

var dataGridTextColumn = new DataGridTextColumn();
string bindingPath = $"{descr.BindingPropertyName}";
Binding b = new Binding(bindingPath);
b.Mode = readOnly ? BindingMode.OneWay : BindingMode.TwoWay;
// Accordng to some articles on StackOverflow, binding as UpdatePropertyChanged
//  prevents the editing of decimal and decimal? values.
//  With UpdateSourceTrigger.LostFocus, it appears to work as desired.
b.UpdateSourceTrigger = UpdateSourceTrigger.LostFocus;
dataGridTextColumn.Binding = b;

The column works as desired, except when the bound property is decimal? (and maybe other types) the user can't leave the cell empty.


Solution

  • You should define a nullable property (decimal?) in the VM and specify the TargetNullValue ('') in the binding, for example:

    b.TargetNullValue = string.Empty;