It works fine but when I am trying to clear a cell I am geting "Value Cannot be converted" validation error.
My binding is to the nullable double.
Please advise what to do.
thanks
public class NumericTextBoxColumn :GridViewBoundColumnBase
{
public override FrameworkElement CreateCellEditElement(GridViewCell cell, object dataItem)
{
this.BindingTarget = NumericTextBox.TextProperty;
NumericTextBox control = new NumericTextBox();
control.SetBinding(this.BindingTarget, this.CreateValueBinding());
return control;
}
private Binding CreateValueBinding()
{
Binding valueBinding = new Binding();
valueBinding.Mode = BindingMode.TwoWay;
valueBinding.NotifyOnValidationError = true;
valueBinding.ValidatesOnExceptions = true;
valueBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
valueBinding.Path = new PropertyPath(this.DataMemberBinding.Path.Path);
return valueBinding;
}
}
where;
public class NumericTextBox: TextBox
{
protected override void OnPreviewTextInput(System.Windows.Input.TextCompositionEventArgs e)
{
if(string.IsNullOrEmpty(e.Text))
{
e.Handled = true;
return;
}
e.Handled = !AreAllValidNumericChars(e.Text);
base.OnPreviewTextInput(e);
}
private bool AreAllValidNumericChars(string str)
{
foreach (char c in str)
{
if (c == '.' && base.Text.Contains(".")) return false;
if (!(Char.IsNumber(c) || c=='.' )) return false;
}
return true;
}
}