AddErrorResult not updating PropertyStatus Control

AddErrorResult not updating PropertyStatus Control

Old forum URL: forums.lhotka.net/forums/t/9450.aspx


Andreas posted on Monday, August 30, 2010

Hi,

When I'am calling BusinessRules.CheckRules() on my Business Object that causes a call on context.AddErroResult() in one of it's BusinessRules the PropertyStatus Control (attached to the invalid property) does not indicate this error (UpdateStatus() of PropertyStatus is not called). I was able to reproduce this behavior in the PropertStatus sample of csla401. Do I miss something? 

  public class Blah : BusinessBase
  {
    public static readonly PropertyInfo<string> DataProperty = RegisterProperty(typeof(Blah), new PropertyInfo<string>("Data"));
    public string Data
    {
      get { return GetProperty(DataProperty); }
      set { SetProperty(DataProperty, value); }
    }

    protected override void AddBusinessRules()
    {
        BusinessRules.AddRule(new TestRule(DataProperty));
    }

    public bool Error;

    public void CheckRules()
    {
      this.Error = true;
      base.BusinessRules.CheckRules();
    }
         
    public class TestRule : Csla.Rules.BusinessRule
    {
      public TestRule(Csla.Core.IPropertyInfo primaryProperty)
      : base(primaryProperty)
      {
          InputProperties = new List<IPropertyInfo> { PrimaryProperty };
      }
      protected override void Execute(Csla.Rules.RuleContext context)
      {
         if((context.Target as Blah).Error == true)
            context.AddErrorResult(primaryProperty, "Error");
      }
    }
  }

   private void Button_Click(object sender, RoutedEventArgs e)
   {
     _ex.CheckRules();
   }


 

Regards,
Andreas

 

RockfordLhotka replied on Monday, August 30, 2010

A couple things.

First, I strongly recommend using the RegisterProperty() overloads that use lambda expressions - the overload you are using often causes maintenance issues due to copy-paste errors over time.

Second, CheckRules() just checks the rules, it doesn't cause any PropertyChanged events to be raised. PropertyStatus (and data binding in general) refreshes the display based on PropertyChanged events. In your method, try calling OnUnknownPropertyChanged() after the CheckRules() call to force that event to be raised for all your properties - that will cause the UI to refresh.

Copyright (c) Marimer LLC