How to tigger PropertyStatus UpdateStatus on demand?

How to tigger PropertyStatus UpdateStatus on demand?

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


Andreas posted on Tuesday, February 09, 2010

Hi,

I need a way to delay the validation of properties for a web form like use case where the validation rules should be executed only

1. when a single property is changed during user input

2. On demand, for example the user wants to proceed to the next step by pressing the “Next Step”-Button. At that point at time all fields of the form should be validated and PropertyStatus should indicate broken rules to the user

The first one was easy: I set SuppressRuleChecking=true by overriding Child_Create

        protected override void Child_Create()

        {

            this.ValidationRules.SuppressRuleChecking = true;

            base.Child_Create();

            this.ValidationRules.SuppressRuleChecking = false;

        }

But I couldn’t figure out a way how to trigger UpdateStatus() later on without forcing a PropertyChanged events for each property. Calling CheckRules doesn’t have any effect on PropertyStatus. Any ideas?

Regards,

Andreas 

Andreas replied on Wednesday, February 10, 2010

Hi,

here is my fix to get the required behavior: I simply added/removed a ListChanged handler to the BrokenRulesCollection in AttachSource()/DetachSource() of PropertyStatus.cs. It does exactly what I want. Any problems related with it? If not, should it become a feature request for CSLA4?

    private void AttachSource(object source)

    {

      this._source = source;

      INotifyBusy busy = source as INotifyBusy;

      if (busy != null)

        busy.BusyChanged += new BusyChangedEventHandler(source_BusyChanged);

 

      INotifyPropertyChanged changed = source as INotifyPropertyChanged;

      if (changed != null)

        changed.PropertyChanged += new PropertyChangedEventHandler(source_PropertyChanged);

 

      BusinessBase businessObject = _source as BusinessBase;

      if (businessObject != null)

      {

          businessObject.BrokenRulesCollection.ListChanged -= BrokenRulesCollection_ListChanged;

          businessObject.BrokenRulesCollection.ListChanged += BrokenRulesCollection_ListChanged;

      }

    }

 

    private void DetachSource(object source)

    {

      INotifyBusy busy = source as INotifyBusy;

      if (busy != null)

        busy.BusyChanged -= new BusyChangedEventHandler(source_BusyChanged);

 

      INotifyPropertyChanged changed = source as INotifyPropertyChanged;

      if (changed != null)

        changed.PropertyChanged -= new PropertyChangedEventHandler(source_PropertyChanged);

 

      BusinessBase businessObject = _source as BusinessBase;

      if(businessObject!=null)

          businessObject.BrokenRulesCollection.ListChanged -= BrokenRulesCollection_ListChanged;

 

    }

 

    void BrokenRulesCollection_ListChanged(object sender, ListChangedEventArgs e)

    {

        this.UpdateState();

    }

 

RockfordLhotka replied on Friday, February 12, 2010

Try calling OnUnknownPropertyChanged() after CheckRules() - that should raise appropriate PropertyChanged events to get the PropertyStatus controls to update.

BrokenRulesCollection and its behavior will likely change quite a bit in CSLA 4, so I'm not convinced it is wise to listen to its change events like that - it will probably cause some odd "UI flicker" effects in the future - though I can't say for sure since I haven't gotten quite that far in the business rules rewrite.

Copyright (c) Marimer LLC