c# - MVVM Command selects the last added item -
i have viewmodel command. last added item editable command. makes sense when @ ode. it's not want. want selected item edited. i'll sketch problem:
i have model, named part
public class part { private string _partcode; private string _description; public string partcode { { return _partcode.trim(); } set { _partcode = value; } } public string description { { return _description; } set { _description = value; } } }
a viewmodel command, named partviewmodel
/// <summary> /// returns viewmodel containing parts. /// </summary> /// <param name="dt">database use.</param> public partviewmodel(databasetype dt) { generateviewmodelforallparts(dt); } private async void generateviewmodelforallparts(databasetype dt) { using (nexuswcfserviceclient client = new nexuswcfserviceclient()) foreach (part item in await client.getallpartsasync(dt)) { _part = item; _items.add(item); } } #endregion #region members private observablecollection<part> _items = new observablecollection<part>(); private part _part; int count = 0; #endregion #region properties public observablecollection<part> items { { return _items; } set { _items = value; } } public part part { { return _part; } set { _part = value; } } public string partcode { { return part.partcode; } set { if (part.partcode != value) /* check if value changed */ { part.partcode = value; raisepropertychanged("partcode"); /* raise event */ } } } public string description { { return part.description; } set { if (part.description != value) { part.description = value; raisepropertychanged("description"); } } } #region commands private void updatedescriptionexecute() { //count++; //description = description + count.tostring(); // part.description = "asdasdasd"; messagebox.show(partcode); } private bool canupdatedescriptionexecute() { if (count >= 2) return false; else return true; } public icommand updatedescription { { return new relaycommand(updatedescriptionexecute, canupdatedescriptionexecute); } } #endregion }
now whenever try fire command in de view:
<window x:class="nexuswpf.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="mainwindow" height="700" width="525" x:name="testview"> <grid> <stackpanel x:name="stck"> <listview x:name="lb" itemssource="{binding items}"> <listview.itemtemplate> <datatemplate> <stackpanel orientation="vertical" x:name="stck"> <textblock text="{binding path=partcode}"/> <textblock text="{binding path=description}"/> <button content="update" datacontext="{binding elementname=testview, path=datacontext}" command="{binding updatedescription}" commandparameter="{binding}"/> </stackpanel> </datatemplate> </listview.itemtemplate> </listview> </stackpanel> </grid> </window>
the last added item (part) edited. makes sense because of following code in viewmodel:
_part = item;
how bind command selected item. guess there must wrong viewmodel , not in binding. _part get's overwritten everytime new part added. how can change work?
use relaycommand<part>
accepts part
command parameter:
public icommand updatedescription { { return new relaycommand<part>(updatedescriptionexecute, canupdatedescriptionexecute); } } private void updatedescriptionexecute(part part) { messagebox.show(part.partcode); } private bool canupdatedescriptionexecute(part part) { if (count >= 2) return false; else return true; }
and modify command binding bit:
<button content="update" command="{binding datacontext.updatedescription,elementname=testview}" commandparameter="{binding}"/>
Comments
Post a Comment