Reputation: 776
I have a Datagrid col binding a nullable int.
<DataGridTextColumn Header = "OVR QTY" Width = "5*" Binding = "{Binding OVERRIDE_QTY, UpdateSourceTrigger = PropertyChanged, StringFormat=N0}" ElementStyle="{StaticResource STRIKE_THROUGH}">
<DataGridTextColumn.EditingElementStyle>
<Style TargetType="{x:Type TextBox}">
<EventSetter Event="PreviewTextInput" Handler="ODQTY_PreviewTextInput">
</EventSetter>
</Style>
</DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>
In the code, I try to limit it to numbers and null.
Regex regex = new Regex("^[.][0-9]+$|^[0-9]*[.]{0,1}[0-9]*$|^[0-9]*[ ][0-9]*[/]{0,1}[1-9]*$|^[1-9]*[/]{0,1}[1-9]*$");
private void ODQTY_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
TextBox tb = sender as TextBox;
var result = tb.Text.Insert(tb.SelectionStart, e.Text);
e.Handled = string.IsNullOrEmpty(result)
|| !regex.IsMatch(result);
}
When I type 1 in the cell, and then type backspace to remove the 1, the datagrid cell won't accept the blank, which I think it is null (correct if I am wrong). Not sure why and how to make it accept null in the cell.
Upvotes: 2
Views: 807
Reputation: 301
In your xaml
<DataGridTextColumn Header = "OVR QTY" Width = "5*" Binding = "{Binding OVERRIDE_QTY, UpdateSourceTrigger = PropertyChanged, .......
Add this:
TargetNullValue=''
Look like this
<DataGridTextColumn Header = "OVR QTY" Width = "5*" Binding = "{Binding OVERRIDE_QTY, UpdateSourceTrigger = PropertyChanged, TargetNullValue='', StringFormat=N0}" ElementStyle="{StaticResource STRIKE_THROUGH}">
I think it'll work.
Upvotes: 2