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
IDataErrorInfoInterface or theINotifyDataErrorInfoInterface 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
IDataErrorInfoInterface, you have an indexer property that you need to implement that takes in astringproperty name. It can be used like this:When implementing the
INotifyDataErrorInfoInterface, you useDataAnnotationattributes on each property, like this:Please search online for further information on implementing these interfaces.