I'm new in xaml and I have some trouble with a textbox bound to an int property.
My property is defined like this :
public Int32 MyInt
{
set
{
if(MyInt!= value)
{
_myInt = value;
OnSpecificModification();
}
}
get
{
return _myInt ;
}
}
private Int32 _myInt = 100;
I have a texbox in my xaml bound to MyInt as follows :
<TextBox PreviewTextInput="NumberValidationTextBox" x:Name="txtDocSteps" x:FieldModifier="private" Text="{Binding Path=DocSteps, UpdateSourceTrigger=PropertyChanged}"/>
The NumberValidationTextBox is to allow only number in the textbox and is :
private void NumberValidationTextBox(object sender, TextCompositionEventArgs e)
{
Regex regex = new Regex("[^0-9]+");
e.Handled = regex.IsMatch(e.Text);
}
All works fine when I put a number in the textBox but when I clear it, then I have an error message "The '' value can't be converted." and I understand why : because MyInt is not nullable. I don't want a nullable int because I need a value. I would like that the value defined in my property (100) replace the empty value in my textbox when I move to an another field or when I click on the validate button which the event on the click is :
private void ButtonValidate_Click(object sender, RoutedEventArgs e)
{
Plug.MyInt = int.Parse(txtDocSteps.Text);
Plug.Description = txtUserDescription.Text;
DialogResult = true;
}
Thank you for your help with simple explanation to understand how to do.