I copied the same approach as with Silverlight on desktops: INotifyDataErrorInfo .
Here I described it more specifically, and here you can download the source code for a sample project.
The simplest example looks like this:
View.xaml
<TextBox Text="{Binding SomeProperty, Mode=TwoWay, ValidatesOnNotifyDataErrors=True, NotifyOnValidationError=True}" Style="{StaticResource ValidationTextBoxStyle}" />
View.xaml.cs
public MainPage() { InitializeComponent(); this.BindingValidationError += MainPage_BindingValidationError; } private void MainPage_BindingValidationError(object sender, ValidationErrorEventArgs e) { var state = e.Action == ValidationErrorEventAction.Added ? "Invalid" : "Valid"; VisualStateManager.GoToState((Control)e.OriginalSource, state, false); }
ViewModel.cs
public class MainViewModel : ValidationViewModel { public MainViewModel() { this.Validator.AddValidationFor(() => this.SomeProperty).NotEmpty().Show("Enter a value"); } private string someProperty; public string SomeProperty { get { return someProperty; } set { someProperty = value; RaisePropertyChanged("SomeProperty"); } } }
It relies on many additional classes, but at the same time there is little code that you write yourself.
source share