Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I properly use .NET Data Annotations for data validation in WPF with a WCF backend?
    primarykey
    data
    text
    <p>Question title pretty much explains what I am trying to do.</p> <p>Simplification of my code for example purpose:</p> <p>Bits of an example WCF Service:</p> <pre><code> pulic class Restaurant { //RegEx to only allow alpha characters with a max length of 40 //Pardon if my regex is slightly off [RegularExpression(@"^[a-zA-Z''-'\s]{1,40}$")] public string Name { get; set; } } public class RestaurantService { List&lt;Restaurant&gt; restaurants = new List&lt;Restaurant&gt;(); public AddRestaurant(string name) { Restaurant restaurant = new Restaurant(); restaurant.Name = name; restaurants.Add(restaurant); } } </code></pre> <p>Bits of example XAML:</p> <pre><code> &lt;TextBox name="txt1" Text="{Binding Restaurant.Name, ValidatesOnDataErrors=True}"/&gt; </code></pre> <p>How do I make my view do something when my data annotation is violated?</p> <p>All of the examples I can find here and elsewhere are either not exactly what I am looking for or that have to do with ASP.NET. I don't know enough about WPF and Data Annotations and I am very green with WCF.</p> <p>I have tried implementing the IDataErrorInfo interface, but I can't seem to get anything to fire in it. I found this code in another different question on StackOverflow. I implemented this in my Restaurant class in the WCF service.</p> <pre><code> public string this[string columnName] { get { if (columnName == "Name") { return ValidateProperty(this.Name, columnName); } return null; } } protected string ValidateProperty(object value, string propertyName) { var info = this.GetType().GetProperty(propertyName); IEnumerable&lt;string&gt; errorInfos = (from va in info.GetCustomAttributes(true).OfType&lt;ValidationAttribute&gt;() where !va.IsValid(value) select va.FormatErrorMessage(string.Empty)).ToList(); if (errorInfos.Count() &gt; 0) { return errorInfos.FirstOrDefault&lt;string&gt;(); } return null; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload