Reputation: 15
For several hours I try to do data validation in a Windows Phone 8.1 application (Windows Runtime) as shown on this page: https://msdn.microsoft.com/en-us/library/windows/apps/cc278072(v=vs.105).aspx#BKMK_Datavalidation.
But it seems that this technique is only valid for WP 8.1 Silverlight (ValidatesOnExceptions and some others properties have been removed from Binding type).
So, I have a “Host” class implementing “INotifyPropertyChanged” interface. Each property is made like this:
public string Name
{
get { return _name; }
set
{
if (string.IsNullOrWhiteSpace(value))
throw new ArgumentException("The name can't be empty.");
// else
_name = value;
NotifyPropertyChanged("Name");
}
}
private string _name;
I would like to take advantage of data binding to make a simple validation data. When a TextBox loses focus, an attempt to change the source is made. If an exception is thrown, the TextBox changes color (for example) until a correct value is entered.
<TextBlock Text="Name" Style="{StaticResource BodyTextBlockStyle}" />
<TextBox x:Name="TextBoxName" Text="{Binding Name, Mode=TwoWay}" Margin="0,8,0,16"/>
Could someone tell me how to get this result (simply if possible) with Windows Runtime?
Upvotes: 0
Views: 1185
Reputation: 21899
Windows Phone 8.1 Runtime apps don't have anything built in for validation. As you note, the docs you linked are specific to Silverlight.
You can implement a basic validation system by giving your data context an IsValid property and binding the TextBox's Foreground colour, validation text, etc. to IsValid with appropriate data converters. A step up would be to write a ValidatingTextBox control which shifts visual states when invalid so it can animate to show the validation notification.
Whatever you do, don't rely only on colour to show the validation status. Remember that not everybody can differentiate colours easily!
Upvotes: 1