LoadProperty Not Triggering PropertyHasChanged

LoadProperty Not Triggering PropertyHasChanged

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


thaehn posted on Thursday, April 30, 2009

Hi -

I am loading a property that was null with a value based upon a user's select in the UI (Using a business rule).  I make a call to LoadProperty like this:

 

target.LoadProperty<DateTime?>(EndedProperty, DateTime.Now);

 

What is interesting is that the value after being loaded doesn't display in the UI.  I need to tell the UI that the property is changed :

target.OnPropertyChanged("Ended");

 

The LoadProperty method's header comment says that it calls PropertyHasChanged, but in the call to LoadPropertyValue the markDirty parameter is passed as false, so the PropertyhasChanged doesn't get called:

/// <summary>

/// Loads a property's managed field with the

/// supplied value calling PropertyHasChanged

/// if the value does change.

/// </summary>

/// <typeparam name="P">

/// Type of the property.

/// </typeparam>

/// <param name="propertyInfo">

/// PropertyInfo object containing property metadata.</param>

/// <param name="newValue">

/// The new value for the property.</param>

/// <remarks>

/// No authorization checks occur when this method is called,

/// and no PropertyChanging or PropertyChanged events are raised.

/// Loading values does not cause validation rules to be

/// invoked.

/// </remarks>

protected void LoadProperty<P>(PropertyInfo<P> propertyInfo, P newValue)

{

   try

      {

         P oldValue = default(P);

         var fieldData = FieldManager.GetFieldData(propertyInfo);

         if (fieldData == null)

         {

            oldValue = propertyInfo.DefaultValue;

            fieldData = FieldManager.LoadFieldData<P>(propertyInfo, oldValue);

        }

        else

        {

           var fd = fieldData as FieldManager.IFieldData<P>;

           if (fd != null)

           oldValue = fd.Value;

           else

           oldValue = (P)fieldData.Value;

        }

        LoadPropertyValue<P>(propertyInfo, oldValue, newValue, false);

   }

   catch (Exception ex)

   {

      throw new PropertyLoadException(

      string.Format(Properties.Resources.PropertyLoadException, propertyInfo.Name, ex.Message), ex);

   }

}

 

Should I be calling something else besides LoadProperty() to change a value from null to DateTime.Now?

Todd

RockfordLhotka replied on Thursday, April 30, 2009

LoadProperty() does not raise PropertyChanged, or check validation or authorization or run business rules. It is as close as possible to just "setting a field".

SetProperty() does all the normal "setting a property" processing.

If you want to set a field (LoadProperty()) and also raise PropertyChanged, then you should manually call OnPropertyChanged() after LoadProperty().

Copyright (c) Marimer LLC