IsDirty property changed

Topics: Feature requests
Oct 29, 2012 at 1:50 PM

Hi,

I have a problem with the property-changed event for the IsDirty property of the DataObjectBase<T> class.

Check the following code:

class Test : DataObjectBase<Test>
{
    #region PROPERTY Name
    public string Name
    {
        get { return GetValue<string>(NameProperty); }
        set { SetValue(NameProperty, value); }
    }

    public static readonly PropertyData NameProperty = RegisterProperty("Name", typeof(string), default(string));
    #endregion

    public Test()
    {

    }


    internal void ClearIsDirty()
    {
        IsDirty = false;
        RaisePropertyChanged(() => IsDirty);
    }
}

class Program
{
    static void Main(string[] args)
    {
        Test t = new Test();
        ((INotifyPropertyChanged)t).PropertyChanged += new PropertyChangedEventHandler(Program_PropertyChanged);
        ((IEditableObject)t).BeginEdit();

        Console.WriteLine("Init. IsDirty: " + t.IsDirty);
        t.Name = "Me";
        Console.WriteLine("After assign. IsDirty: " + t.IsDirty);
        t.ClearIsDirty();
        Console.WriteLine("After clear: IsDirty: " + t.IsDirty);

        ((IEditableObject)t).CancelEdit();

        Console.WriteLine("After cancel: IsDirty: " + t.IsDirty);

        Console.ReadLine();
    }

    static void Program_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        Console.WriteLine(" - {0} changed", e.PropertyName);
    }
}

Here I clear the IsDirty flag in my class by setting it to False, but this does not fire a NotifyPropertyChanged event. Event if I try to fire it manually with RaisePropertyChanged(...).

Next to this I have a phenomenon that I cannot reproduce (yet) with an example project. Calling CancelEdit() in my project fires the PropertyChanged events for the properties that are reset property but does not fire the IsDirty-PropertyChanged even though the IsDirty property changed.

Any ideas about these topics (I will try to narrow down the second problem).

Thanks, Gorroux

Oct 29, 2012 at 1:51 PM

This is the result of the test program:

Init. IsDirty: False
 - Name changed
 - IsDirty changed
After assign. IsDirty: True
After clear: IsDirty: False
 - Name changed
 - IsDirty changed
After cancel: IsDirty: False

One "IsDirty changed" is missing right before "After clear".

Oct 31, 2012 at 10:29 PM

You want the ClearIsDirty to invoke the PropertyChanged event, is that correct?

Nov 5, 2012 at 9:01 AM

Yes, thats correct.

Nov 7, 2012 at 6:56 PM

It's good behavior. I wrote a unit test, here it is:

            [TestMethod]
            public void CorrectlyRaisesPropertyChangedForIsDirty()
            {
                int isDirtyChangedCalls = 0;
                var model = new ClearIsDirtyModel();

                ((INotifyPropertyChanged)model).PropertyChanged += (sender, e) =>
                {
                    if (string.Equals(e.PropertyName, "IsDirty"))
                    {
                        isDirtyChangedCalls++;
                    }
                };

                ((IEditableObject)model).BeginEdit();

                // IsDirty change 1
                model.Name = "Me";

                // IsDirty change 2
                model.ClearIsDirty();

                // IsDirty change 3
                ((IEditableObject)model).CancelEdit();

                Assert.AreEqual(3, isDirtyChangedCalls);
            }

 

As you can see, IsDirty change 3 is not a change. At begin edit, the IsDirty is false. Because you cleared it at change 2 (ClearIsDirty), it will not raise a property change because the IsDirty has actually not changed.