c# - Binding Command to ItemsControl Item -
i working on win10 uwp app using mvvmlight (i've never used mvvmlight, , never done commands "properly"). have itemscontrol bound observablecollection. participant has 2 properties - name , laps. have controls in itemscontrol.itemtemplate display button (subtract), name property of item, laps property of item, , button (add). here xaml:
<itemscontrol itemssource="{binding participants, mode= twoway}"> <itemscontrol.itemtemplate> <datatemplate> <grid minwidth="300" margin="0, 12"> <grid.columndefinitions> <columndefinition width="2*"></columndefinition> <columndefinition width="6*"></columndefinition> <columndefinition width="2*"></columndefinition> <columndefinition width="3*"></columndefinition> </grid.columndefinitions> <button content="" fontfamily="segoe mdl2 assets" fontsize="20" grid.column="0" margin="0, 0, 12, 0"></button> <textblock text="{binding path=name, mode=twoway}" fontsize="20" fontweight="bold" grid.column="1"></textblock> <textblock text="{binding laps, mode=twoway}" fontsize="20" fontweight="bold" grid.column="2"></textblock> <button content="" fontfamily="segoe mdl2 assets" fontsize="20" command="{binding addlapcommand}" commandparameter="{binding}" grid.column="3" margin="12, 0, 0, 0"></button> </grid> </datatemplate> </itemscontrol.itemtemplate> </itemscontrol>
the view model creates observablecollection called participants. trying bind add button (and of course subtract button) relaycommand in vm. i've tried number of things, can't post i've tried here. here latest of have, (but still doesn't work):
public relaycommand<object> addlapcommand { { if (_addlapcommand == null) { _addlapcommand = new relaycommand<object>((e) => executeaddlapcommand(e)); } return _addlapcommand; } } private void executeaddlapcommand(object o) { throw new notimplementedexception(); }
the intellisense in xaml tells me cannot resolve property addlapcommand in data context of type lapcounter.model.participant. i'm not trying participant class, homeviewmodel class, datacontext of page. think can see getting lapcounter.model.participant from, individual item in collection itemscontrol bound to. under impression if datacontext couldn't found, continue visual tree until found right one. here's datacontext in page declaration:
datacontext="{binding home, source={staticresource locator}}"
how @ vm relaycommand? need have button send participant represents relaycommand parameter, , use increment (and decrement in case of subtract button) laps integer particular participant.
i appreciate can get. thanks!
edit added participants property vm show because view not updating. here participants property:
/// <summary> /// <see cref="participants" /> property's name. /// </summary> public const string participantspropertyname = "participants"; private observablecollection<participant> _participants = new observablecollection<participant>(); /// <summary> /// sets , gets participants property. /// changes property's value raise propertychanged event. /// </summary> public observablecollection<participant> participants { { return _participants; } set { if (_participants == value) { return; } _participants = value; raisepropertychanged(() => participants); } }
thanks eldar dordzhiev far!
try this:
command="{binding home.addlapcommand, source={staticresource locator}}"
there's not explain long you've written absolutely right.
as incrementing\decrementing laps
, can pass participant
command handler , alter laps
. if use mvvmlight done relaycommand<participant>
. don't forget pass participant
in commandparameter
.
Comments
Post a Comment