2

I have a WPF view that displays a Shipment entity. I have a textblock that contains an asterisk which will alert a user that the record is changed but unsaved. I originally hoped to bind the visibility of this (with converter) to the Shipment.EntityState property.

If value = EntityState.Modified Then
    Return Visibility.Visible
Else
    Return Visibility.Collapsed
End If

The property gets updated just fine, but the view is ignorant of the change. What I need to know is, how can I get the UI to receive notification of the property change. If this cannot be done, is there a good way of writing my own IsDirty property that handles editing retractions (i.e. if I change the value of a property, then change it back to it's original it does not get counted as an edit, and state remains Unchanged).

Any help, as always, will be greatly appreciated.

Cory

2 Answers 2

2

After struggling with the same problem for a little bit, here is a solution that is working for me. Lets say I have an entity called Trip that was generated by EF, I just needed to extend the class by means of partial class as showed below. The RaiseEntityStateChanged method is useful when you need to force a refresh of the EntytyState property, for example after calling the context's SaveChanges method.

partial class Trip
{
   bool _forced = false;
   System.Data.EntityState _lastState;

   public Trip()
   {
     _lastState = EntityState;
     this.PropertyChanged += (s, e) => 
     {
       if (_lastState != this.EntityState && e.PropertyName != "EntityState" || _forced)
       {
          _forced = false;
          OnPropertyChanged("EntityState");
       }

       _lastState = this.EntityState;
    };
  }

  public virtual void RaiseEntityStateChanged()
  {
     _forced = true;
     OnPropertyChanged("EntityState");
  }
}
1

I don't see a way to create a XAML binding on an existing property to do what you are trying to do. But you could write your own IsDirty property, based on the EntityState; you could update this value by subscribing to the PropertyChanged event raised by the base EntityObject. Of course, you'll need to also raise a PropertyChanged event for IsDirty (so that the GUI is notified) and ignore this event in your handler (to prevent infinite recursion). Edit: added the following after question by OP:

This is how I see it, in order to answer the comment.

In the shipment class, one can add:

public bool IsDirty { get { return EntityState == EntityState.Modified; } }

public Shipment() {
...
PropertyChanged += OnShipmentChanged;
}

private void OnShipmentChanged(object sender, PropertyChangedEventArgs pcea) {
    if (pcea.PropertyName != "IsDirty") { // prevent recursion
        OnPropertyChanged("IsDirty"); // notifies binding listener that the state has changed
    }
}

During the night, I thought of another way, which is to create a multi-binding on each Shipment property (which would replace this whole notion of an IsDirty property and would actually answer the original question). This could make sense if there are just a couple of Shipment properties. I'd say if there are more than 3, we should forget about this idea.

3
  • How does one listen for an event raised in another class, like an entity? I tried to create an event handler in the shipment entity class and then in the ViewModel, with no luck. Sorry for asking this, but I just don't know and the Google is not being kind to me this morning. Cory Apr 9, 2010 at 14:58
  • I am translating your above code to VB.NET and I am stuck on This :PropertyChanged += OnShipmentChanged;. What is that doing? I understand that += just adds the indicated value to the current value, but the debugger is taking exception to that. Apr 9, 2010 at 16:58
  • 1
    Oops, sorry about the language. += with events means to subscribe to the event. I am not fluent in VB.NET, but I think that the equivalent is: AddEventHandler(event, AddressOf handling_method)
    – Timores
    Apr 10, 2010 at 7:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.