c# - How do I recognize changes to an object using and EF6 Generated class in order to implement CanExecute? -
i binding button in xaml update user in database. want utilize canexecute enable button after change has been made property on viewmodel.
i using entity framework , wcf data service perform crud operations.
model class
public virtual dbset<user> users { get; set; } ... namespace pk.data { using system; using system.collections.generic; public partial class user { public user() { this.private_account = new hashset<private_account>(); } public int id { get; set; } public string username { get; set; } public int superuser { get; set; } public int enabled { get; set; } public virtual icollection<private_account> private_account { get; set; } } }
wcf service
public class pkservice { // getusers() - // users database [operationcontract] public ienumerable<user> getusers() { using (var context = new pkentities()) { // needed in order handle ef objects hashes context.configuration.proxycreationenabled = false; // pull entity object fom db var results = context.users.tolist(); // detatch entity model results.foreach(e => context.entry(e).state = entitystate.detached); //return detached object return results; } } // updateuser(user) - // update user in db [operationcontract] public void updateuser(user user){ using (var context = new pkentities()) { context.configuration.proxycreationenabled = false; context.entry(user).state = entitystate.modified; context.savechanges(); } } }
view model #
public class homeviewmodel: viewmodelbase { // homeviewmodel() // class constructor public homeviewmodel() { this.refreshusers(); userlistclick = new relaycommand<selectionchangedeventargs>(_userlistclick); saveuser = new relaycommand(saveuser); } // users // list of users in system private ienumerable<user> _users; public ienumerable<user> users { { return this._users; } set { this._users = value; this.raisepropertychanged("users"); } } // selected user // selected user private user _selecteduser; public user selecteduser { { return this._selecteduser; } set { if (this._selecteduser != value) { this._selecteduser = value; this.raisepropertychanged("selecteduser"); } } } // refreshusers() // method retrieving users db private void refreshusers() { this._pkserviceclient.getuserscompleted += (s, e) => { this.users = e.result; }; this._pkserviceclient.getusersasync(); } // serverlistclick public icommand userlistclick { get; private set; } private void _userlistclick(selectionchangedeventargs e) { listview userlist = (listview)e.source; _selecteduser = (user)userlist.selecteditem; this.setuserdetailsvisible(visibility.visible); this.raisepropertychanged("selecteduser"); } public icommand saveuser { get; private set; } private void _saveuser() { this._pkserviceclient.updateuser(_selecteduser); } } }
i believe possible tie relaycommand(saveuser, somecommand) , have somecommand return value can bind in xaml control enabled state of button.
i cannot seem find way using entity generated class handle this. assistance wonderful.
if understand correctly, want enable button connected saveuser command soon, there changes made selecteduser?
i believe possible tie relaycommand(saveuser, somecommand) , have somecommand return value can bind in xaml control enabled state of button.
this possible compositecommands (msdn documentation). here can call method "registercommand" , pass second command affects behavior of button connected compositecommand. if registered button returns canexecute == false, button disabled.
but add canexecute method relaycommand , check there, if entity has correct value. example, canexecute method check example username:
public homeviewmodel() { this.refreshusers(); userlistclick = new relaycommand<selectionchangedeventargs>(_userlistclick); saveuser = new relaycommand(saveuser, canexecutesaveuser); } private bool canexecutesaveuser() { if (selecteduser.username == "foo") { return true; } return false; }
i not connect directly model. model should not bothered logic needed validation of ui. should add additional layer between viewmodel , wcf service, kind of userviewmodel can handle changes made properties of user directly. changes in these properties can trigger raisecanexecutechanged method of relaycommand.
i hope helps, if misunderstood you, please provide further explanation of problem.
edit:
after comment, think can implement inotifypropertychanged event , subscribe these events. this, necessary add additional layer of userviewmodel application. stripped example:
public homeviewmodel() { this.refreshusers(); userlistclick = new relaycommand<selectionchangedeventargs>(_userlistclick); saveuser = new relaycommand(saveuser, canexecutesaveuser); selecteduserviewmodel.propertychanged += selecteduseronpropertychanged; } private void selecteduseronpropertychanged(object sender, propertychangedeventargs propertychangedeventargs) { if (propertychangedeventargs.propertyname.equals(nameof(userviewmodel.username))) { (saveuser relaycommand)?.raisecanexecutechanged(); } }
Comments
Post a Comment