views:

85

answers:

1

In a WPF / EF4.0 / MVVM app I have a View that edits a Customer entity. What is the best way to set a property "bool IsCustomerInEditMode" in my CustomerViewModel short of acting upon OnChanging/OnChanged partial methods for every single individual property of the Entity? As far as I know there's no OnEntityChanging method...

Thanks!

Edit: Answer: EntityState

+1  A: 

Edit: To answer your question in your post (best way to set bool IsCustomerInEditMode) -

Subscribe to the entity.PropertyChanging event, inside it set IsCustomerInEditMode == true; Subscribe to the entity.PropertyChanged event, inside it set IsCustomerInEditMode == false;

I think InstanceOfYourCustomer.PropertyChanging and InstanceOfYourCustomer.PropertyChanged the events you're looking for. For every generated property on your entity, the event fires if a property changes (unless you use partial classes to add additional properties to your entity, in which case, you'll need to add calls to ReportPropertyChanging and ReportPropertyChanged in the setters of those properties).

http://msdn.microsoft.com/en-us/library/system.data.objects.dataclasses.structuralobject.propertychanged.aspx

http://msdn.microsoft.com/en-us/library/system.data.objects.dataclasses.structuralobject.propertychanging.aspx

I'm using EF4, and looking at my Model.Designer.cs file... all of my entities' properties' setters call ReportPropertyChanging and ReportPropertyChanged... which will fire the PropertyChanging and PropertyChanged events on your entity, and the args will even tell you which specific property it was that raised the changed event.

Scott
Scott,Thanks for your answer. I can see customerInstance.PropertyChanging event. This may be the key to what I am trying to do. The ReportPropertyChanging calls you see is for individual properties and not the whole Entity.I am not sure yet but I believe what I am looking for is EntityObject.EntityState which reports modified and I could assigned to my IsCustomerInEditMode property.
Gustavo Cavalcanti
Glad you were able to solve the problem! I could be wrong, but I thought ReportPropertyChanging raised the actual entity's PropertyChangingEvent. I realize that the parameter to this call is the string value of the specific property of the entity that is changing, however I think that is so the event args of the PropertyChanging event can specify which property of the entity actualy caused the event to fire (since we have no way of knowing otherwise). Regardless, it does sound like EntityState will be better for you as it appears you want it to be in the 'editing state' until it is committed.
Scott