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
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.
精彩评论