I have a WPF application using MVVM. It has a textbox which needs an integer. The XAML of that text box is as below
<TextBox Name="textBoxElementWeight"
Text="{Binding ElementName=listBoxElement, Path=SelectedItem.Weight,
UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True}"
Validation.ErrorTemplate="{StaticResource ValidationTextBoxTemplate}"/>
The view model implements interface INotifyDataErrorInfo.
When I delete the text to enter a new one, it says "Value '' could not be converted."
How do I change this error message to mine? Ex: "Please enter a number."
The whole Visual Studio solution can be downloaded here
The simplest way to provide custom validation messages is to implement either the
IDataErrorInfo
Interface or theINotifyDataErrorInfo
Interface in your data object class. I won't go into a detailed implementation here because there are many tutorials that are easily found online, however, I'll explain briefly.When implementing the
IDataErrorInfo
Interface, you have an indexer property that you need to implement that takes in astring
property name. It can be used like this:When implementing the
INotifyDataErrorInfo
Interface, you useDataAnnotation
attributes on each property, like this:Please search online for further information on implementing these interfaces.