C# MVVM: Edit a SelectedItem of an ObservableCollection with a RelayCommand -


i quiet new programming , learning c# , mvvmc pattern (which think same mvvm pattern).

i need code database tool chiliplants university. there should able edit existing item observablecollection.

this observablecollection displayed in datagrid, see this:datagrid below datagrid there 3 buttons: add, edit , delete. able programm addbutton, aswell deletebutton.

unfortunately don't know how programm editbutton. should open new window, selecteditem should opened this:editwindow

until editbutton same thing addbutton.

see code here:

view:

<stackpanel grid.row="1" orientation="horizontal">         <button content="add" margin="5,5,0,5" width="100" command="{binding addcommand}" />         <button content="edit" margin="5,5,0,5" width="100" command="{binding editcommand}" />         <button content="delete" margin="5,5,540,5" width="100" command="{binding deletecommand}" />         <button content="sichern" margin="5,5,5,5" width="100" command="{binding savecommand}" /> </stackpanel> 

viewmodel:

    private icommand _editcommand;      public icommand editcommand     {         { return _editcommand; }         set { _editcommand = value; }     } 

controller:

public void sdinitialize()                                               {         var view = new windowstammdatenverwaltung();          mviewmodel = new windowstammdatenverwaltungviewmodel         {             editcommand = new relaycommand(editcommandexecute, editcommandcanexecute)         };         view.datacontext = mviewmodel;         view.showdialog();     }  private void editcommandexecute(object obj)     {         var editedobject = new windoweditcontroller().editchilimodel();         if (editedobject != null)         {             mviewmodel.stock.add(mviewmodel.selectedchili);         }     }  private bool editcommandcanexecute(object obj)     {         return mviewmodel.selectedchili != null;     } 

the problem editcommandexecute. have put code addcommandexecute in there. unfortunately don't know how code such editcommandexecute.

my windoweditcontroller looks this:

public class windoweditcontroller {     windowedit mview;      public chilimodel editchilimodel()     {         mview = new windowedit();         windoweditviewmodel mviewmodel = new windoweditviewmodel         {             chilimodel = new chilimodel(),             okcommand = new relaycommand(executeokcommand),             cancelcommand = new relaycommand(executecancelcommand),         };         mview.datacontext = mviewmodel;         if (mview.showdialog() == true)         {             return mviewmodel.chilimodel;         }         else         {             return null;         }     }      private void executeokcommand(object obj)     {         mview.dialogresult = true;         mview.close();     }      private void executecancelcommand(object obj)     {         mview.dialogresult = false;         mview.close();     } 

i know, let user edit selecteditem inside datagrid, not allowed in task...

could maybe use same window addcommand? should same, editwindow should contain information of selecteditem.

i looked every entry similar topic, did not find simple solution. or solution able understand bad coding skills :( ...

i happy if guys me. please keep simple newbie :)

what tried:

i tried add commandparameter button looking this: commandparameter="{binding selecteditem, elementname=stockdatagrid}" still didn't open window containing data of selecteditem. opened new window new item.

use commandparameter after command property, , bind selecteditem of datagrid.

for example, suppose datagrid has attribute name=mydatagrid.

the button becomes:

<button content="edit"         margin="5,5,0,5"         width="100"         command="{binding editcommand}"         commandparameter="{binding selecteditem, elementname=mydatagrid}"/> 

when editcommandexecute(object obj) executed, obj current selecteditem want.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -