views:

291

answers:

1

Hello, all!

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

+1  A: 

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.

Timores
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
OffApps Cory
Please have a look at the edited answer.
Timores
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.
OffApps Cory
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