c# - Saving Data from a user control WPF MVVM -


i have 2 user controls each contain numericinput added listbox in mainwindow using itemscontrol itemtemplate. these both bound ilist property so, in clientratesviewmodel (the 1 mainwindow) need save each value in numeric input in savedata method, how can achieve this?

mainwindow.xaml(some code removed)

    <listbox x:name="lbpostawr" itemssource="{binding clientratespostawrdescription}"         keyboardnavigation.tabnavigation="continue" margin="548,23,10,69" av:grid.row="1">         <itemscontrol.itemcontainerstyle>             <style targettype="listboxitem">             </style>         </itemscontrol.itemcontainerstyle>         <itemscontrol.itemspanel>             <itemspaneltemplate>                 <stackpanel orientation="vertical"/>             </itemspaneltemplate>         </itemscontrol.itemspanel>         <itemscontrol.itemtemplate>             <datatemplate>                 <grid margin="0" focusable="false">                     <usercontrols:postawr />                 </grid>             </datatemplate>         </itemscontrol.itemtemplate>     </listbox> 

clientratesviewmodel(some code removed)

 public ilist<clientrates> clientratespreawrvalue     {         { return _clientratepreawrvalue; }         set         {             _clientratepreawrvalue = value;             onpropertychanged("clientratespreawrvalue");         }     }      public ilist<clientrates> clientratespostawrvalue     {         { return _clientratepostawrvalue; }         set         {             _clientratepostawrvalue = value;             onpropertychanged("clientratespostawrvalue");         }     }   private bool savedata() ///qqq     {         bool issaved = false;          try         {             using (var connection = new sqlconnection(extensionmethods.connectionstring))             {                 connection.open();                 using (var command = new sqlcommand("sphere", connection))                 {                     command.commandtype = commandtype.storedprocedure;                      //foreach (var test in this.)                     //{                     //    command.parameters.add(rate.clientratespreawr,                     //}                      int rowsaffected = command.executenonquery();                     {                       }                 }             }             issaved = true;         }          catch (exception ex)         {             throw;         }         return issaved;   public ilist<clientrates> clientratespreawrvalue     {         { return _clientratepreawrvalue; }         set         {             _clientratepreawrvalue = value;             onpropertychanged("clientratespreawrvalue");         }     }      public ilist<clientrates> clientratespostawrvalue     {         { return _clientratepostawrvalue; }         set         {             _clientratepostawrvalue = value;             onpropertychanged("clientratespostawrvalue");         }     } 

my clientsrates model looks this:

    private string _clientratespreawrdescription;     private double _clientratepreawrvalue;     private double _clientratepostawrvalue;     private string _clientratepostawrdescription;      public event propertychangedeventhandler propertychanged;      private void onpropertychanged(string propertyname)     {         if (propertychanged != null)         {             propertychanged(this, new propertychangedeventargs(propertyname));         }     }      public string clientratespreawrdescription     {         { return _clientratespreawrdescription; }         set         {             _clientratespreawrdescription = value;             onpropertychanged("clientratespreawr");         }     }      public double clientratespreawrvalue     {         { return _clientratepreawrvalue; }         set         {             _clientratepreawrvalue = value;             onpropertychanged("clientratespreawrvalue");         }     }      public double clientratespostawrvalue     {         { return _clientratepostawrvalue; }         set         {             _clientratepostawrvalue = value;             onpropertychanged("clientratespostawrvalue");         }     }      public string clientratespostawrdescription     {         { return _clientratepostawrdescription; }         set         {             _clientratepostawrdescription = value;             onpropertychanged("clientratespostawr");         }     } } 

listbox's itemssource has bound collection, not string property (clientratespostawrdescription). start following view:

<window.datacontext>     <vm:viewmodel /> <!--or set datacontext in way--> </window.datacontext> <stackpanel>     <listbox x:name="lbpostawr" itemssource="{binding clientratespostawr}">         <itemscontrol.itemcontainerstyle>             <style targettype="listboxitem">             </style>         </itemscontrol.itemcontainerstyle>         <itemscontrol.itemspanel>             <itemspaneltemplate>                 <stackpanel orientation="vertical"/>             </itemspaneltemplate>         </itemscontrol.itemspanel>         <itemscontrol.itemtemplate>             <datatemplate>                 <grid margin="0" focusable="false">                     <grid.columndefinitions>                         <columndefinition />                         <columndefinition />                     </grid.columndefinitions>                     <label grid.column="0" content="{binding description}" />                     <textbox grid.column="1" text="{binding value}" />                 </grid>             </datatemplate>         </itemscontrol.itemtemplate>     </listbox>     <button content="save" command="{binding savecommand, mode=oneway}" /> </stackpanel> 

and view model(s):

public class clientrateviewmodel {     public string description { get; set; }     public int value { get; set; } }  public class relaycommand : icommand {     action<object> _execute;      public relaycommand(action<object> execute) {         _execute = execute;     }      public void execute(object parameter) {         _execute(parameter);     }      public bool canexecute(object parameter) {         return true;     }      public event eventhandler canexecutechanged; }  public class viewmodel {     public viewmodel() {         clientratespostawr = new list<clientrateviewmodel>();         clientratespostawr.add(new clientrateviewmodel { description = "description 1", value = 1 });         clientratespostawr.add(new clientrateviewmodel { description = "description 2", value = 2 });         savecommand = new relaycommand((o) => {             messagebox.show("saving: " + string.join(",", clientratespostawr.select(cr => cr.value.tostring())));         });     }      public ilist<clientrateviewmodel> clientratespostawr { get; private set; }      public icommand savecommand { get; private set; } } 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -