c# - WPF Binding ICommand with ViewModel -


icommand:

public class cmdaddedituser : icommand {     public event eventhandler canexecutechanged;     public vmaddedituser viewmodel { get; set;}      public cmdaddedituser()     {     }      public cmdaddedituser(vmaddedituser vm)     {         viewmodel = vm;     }      public bool canexecute(object parameter)     {         return true;     }       public void execute(object parameter)     {         this.viewmodel.simplemethod();     } } 

viewmodel:

public class vmaddedituser {     private employee _employee = new employee();     private cmdaddedituser command { get; set; }      public vmaddedituser()     {         command = new cmdaddedituser(this);     }      public string txtfirstname     {         { return _employee.firstname; }         set { _employee.firstname = value; }     }      public void simplemethod()     {         txtfirstname = "abc";     } } 

xaml:

<window x:class="wpf.addedituserview"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         xmlns:vm="clr-namespace:viewmodel;assembly=viewmodel"         title="addedituserview" height="392.329" width="534.143">     <grid margin="0,0,2,-3">         <grid.resources>             <vm:vmaddedituser x:key="abx"/>         </grid.resources>         <grid.datacontext>             <vm:vmaddedituser/>         </grid.datacontext>         <button x:name="btn" content="cancel" command="{binding simplemethod, source={staticresource abx}}"/>      </grid> </window> 

the cmdaddedituser , vmaddedituser in same project while xaml in different project.

the .execute(object parameter) of icommand doesn't seem work. can't bind simplemethod button have. when type command binding in xaml file, auto-complete/suggestions shows txtfirstname , not simplemethod. can't figure out why simplemethod can't binded , can't found. did wrong in code?

first: properties want view able bind to, must public. since view binds property, instance of icommand implementation, property must public, view can access it. however, simplemethod() can private if don't wanna expose outside world, why have command calling instead of letting view directly call it. second: set grids datacontext 'vmedituser' class, in binding there no need specify source, datacontext source.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -