Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem you are having is that your DataContext is being set after you have created the validation rule and there is no notification that it has changed. The simplest way to solve the problem is to change the xaml to the following:</p> <pre><code>&lt;TextBox.Text&gt; &lt;Binding NotifyOnValidationError="True" Path="myViewModelProperty" UpdateSourceTrigger="PropertyChanged"&gt; &lt;Binding.ValidationRules&gt; &lt;local:TotalQuantityValidator x:Name="validator" /&gt; &lt;/Binding.ValidationRules&gt; &lt;/Binding&gt; &lt;/TextBox.Text&gt; </code></pre> <p>And then set up the Context directly after setting the DataContext:</p> <pre><code>public MainWindow() { InitializeComponent(); this.DataContext = new MyViewModel(); this.validator.Context = new TotalQuantityValidatorContext { ViewModel = (MyViewModel)this.DataContext }; } </code></pre> <p>You could actually remove the Context class now and just have a property directly on the ValidationRule containing the ViewModel.</p> <p><strong>EDIT</strong></p> <p>Based on your comment I now suggest a slight change to the above code (the XAML is fine) to the following:</p> <pre><code>public MainWindow() { this.DataContextChanged += new DependencyPropertyChangedEventHandler(MainWindow_DataContextChanged); InitializeComponent(); } private void MainWindow_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e) { this.validator.Context = new TotalQuantityValidatorContext { ViewModel = (MyViewModel)this.DataContext }; } </code></pre> <p>This will update your context whenever your viewmodel changes.</p>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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