Use ValidationRules provided by WPF.
The xaml would be:
<TextBox>
<TextBox.Text>
<Binding Path="Name">
<Binding.ValidationRules>
<ExceptionValidationRule />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
The code for the textbox property would be (used regex for the validation):
public string Name
{
get { return _name; }
set
{
_name = value;
if (!Regex.IsMatch(value, @"^((?:[1-9]\d*)|(?:(?=[\d.]+)(?:[1-9]\d*|0)\.\d+))$"))
{
throw new ApplicationException("Please enter only numbers/decimals.");
}
}
}
Source: Validation in WPF
The regex given above: ^((?:[1-9]\d*)|(?:(?=[\d.]+)(?:[1-9]\d*|0)\.\d+))$
can be tested at this Rubular link
The regex would match these:
1.2
22522
0.33
3.90000
but not these: (you could tweak the regex to allow some of them)
.999
23.35.1343
03423.23423