c# - Binding to items in ItemsControl -
i have problem binding. xaml structure in wpf looks this:
<itemscontrol itemssource="{binding cachelist}" > <itemscontrol.itemtemplate> <datatemplate> <stackpanel> <textblock text="{binding type}"/> </stackpanel> </datatemplate> </itemscontrol.itemtemplate> <i:interaction.triggers> <i:eventtrigger eventname="mousedown"> <i:invokecommandaction command="{binding loadfromcache}" commandparameter="-- bind clicked item --"/> </i:eventtrigger> </i:interaction.triggers> </itemscontrol>
and moment works fine. when click on items in itemscontrol notice in modelview class. don't know particular item clicked.
i tried bind "this" object by:
<i:invokecommandaction command="{binding loadcache}" commandparameter="{binding}"/>
but in case strange result, because receive (check in output): app.viewmodels.mainviewmodel, context view, not particular item.
how it, resolve item clicked?
edit: found solution: use listbox (or deriving selector control) in order use selecteditem property. not satisfy me, want stay itemscontrol.
now know want bindings, can take approach.
first, make text box clickable area , create input binding handle mouse click:
<textblock tag="{binding}" text="{binding name}"> <textblock.inputbindings> <mousebinding mouseaction="leftclick" command="{binding itemclick}" commandparameter="{binding}"/> </textblock.inputbindings> </textblock>
you can see mousebinding handles leftclick of mouse , and specified command binds icommand defined in item object type of items control:
public class yourobject { public icommand itemclick { { return new itemclickcommand(); } } public string name { get; set; } }
then command instance "itemclickcommand" fire when leftclick happens - , have item command parameter:
public class itemclickcommand : icommand { public event eventhandler canexecutechanged; public bool canexecute(object parameter) { return true; } public void execute(object parameter) { // parameter item clicked } }
Comments
Post a Comment