Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can't do this functionality in your model or data type class using the <code>IDataErrorInfo</code> interface because you won't have access to the other objects there. Instead, you'll have to do it in your view model. However, you can report the error using that interface. I have extended its functionality by adding a property into my data type base class:</p> <pre><code>public virtual ObservableCollection&lt;string&gt; ExternalErrors { get { return externalErrors; } } </code></pre> <p>As you can see, mine deals with multiple errors, but you can easily change this to:</p> <pre><code>public virtual string ExternalError { get { return externalError; } } </code></pre> <p>Then I 'plug' this into my <code>Errors</code> property:</p> <pre><code>public override ObservableCollection&lt;string&gt; Errors { get { errors = new ObservableCollection&lt;string&gt;(); errors.AddUniqueIfNotEmpty(this["Name"]); errors.AddUniqueIfNotEmpty(this["EmailAddresses"]); errors.AddUniqueIfNotEmpty(this["StatementPrefixes"]); errors.AddRange(ExternalErrors); return errors; } } </code></pre> <p>Again, I have adapted this interface to return multiple errors, but you can change this to:</p> <pre><code>public override string Error { get { error = string.Empty; if ((error = this["Name"])) != string.Empty) return error; if ((error = this["EmailAddresses"])) != string.Empty) return error; if ((error = this["Name"])) != string.Empty) return error; if (ExternalError != string.Empty) return ExternalError; return error; } } </code></pre> <p>Incidentally, it is <em>far</em> more efficient to call just the indexers that you are actually validating rather than your example of calling all properties using reflection. However, that is your choice.</p> <p>So now that we have this <code>ExternalError</code> property, we can use it to display external error messages from your view model (create a class that contains a collection property to bind to the <code>DataGrid.ItemsSource</code> property). </p> <p>If you are using <code>ICommand</code> objects, then you can put this code into the <code>CanExecute</code> method of your Save command:</p> <pre><code>public bool CanSave(object parameter) { clsProducts instance = (clsProducts)parameter; instance.ExternalError = YourCollectionProperty.Contains(instance) ? "The values must be unique" : string.Error; // Perform your can save checks here } </code></pre> <p>Please note that you will need to implement the <code>Equals</code> method in your data type object for this to work. There are many similar ways to achieve this and I trust that from this example you will be able to work one out that works for you.</p>
    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.
    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