Where to raise NotifyPropertyChanged?

admin

Administrator
Staff member
I just implemented my business logic validation according to Rachel Lim's <a href="http://rachel53461.wordpress.com/2012/01/22/validating-business-rules-in-mvvm/" rel="nofollow">blog</a>.
Everything was running great until I decided to put a trigger in my view bound to the <strong>IsValid</strong> property like this:

Code:
&lt;ListBox ItemsSource="{Binding EdgedBoards}" SelectedItem="{Binding SelEdgedBoard, Mode=TwoWay}" DisplayMemberPath="Name"&gt; 
                    &lt;ListBox.ItemContainerStyle&gt;
                        &lt;Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}"&gt;
                            &lt;Setter Property="Focusable" Value="True" /&gt;
                            &lt;Style.Triggers&gt;
                                &lt;DataTrigger Binding="{Binding Path=IsValid}" Value="False"&gt;
                                    &lt;Setter Property="Focusable" Value="False" /&gt;
                                &lt;/DataTrigger&gt;
                            &lt;/Style.Triggers&gt;
                        &lt;/Style&gt;
                    &lt;/ListBox.ItemContainerStyle&gt;
                &lt;/ListBox&gt;

The problem is that when the bound item has an error (SelEdgedBoard.IsValid == false) the trigger is not notified to reevaluate and the item keeps its focusable property to true.

I've already tried to put a NotifyPropertyChanged("IsValid") before the GetValidationError() returns its value, but this way I get a stackoverflow exception:

Code:
#region IsValid Property

    public bool IsValid
    {
        get
        {
            return string.IsNullOrWhiteSpace(GetValidationError());
        }
    }

    public string GetValidationError()
    {
        string error = null;

        if (ValidatedProperties != null)
        {
            foreach (string s in ValidatedProperties)
            {
                error = GetValidationError(s);
                if (!string.IsNullOrWhiteSpace(error))
                {
                    break; 
                }
            }
        }

        NotifyPropertyChanged("IsValid");
        return error;
    }

    #endregion