c# - ListBox SelectionChanged Not Firing -
i have listbox
<listbox grid.columnspan="4" grid.column="1" grid.row="7" grid.rowspan="2" style="{dynamicresource productlistboxstyle}" itemtemplate="{dynamicresource productitemtemplate}" itemssource="{binding relatedproducts}" itemspanel="{dynamicresource productlistitemspaneltemplate}" selecteditem="{binding selectedproduct,mode=twoway}" padding="-12,0,0,0" ishittestvisible="true" itemcontainerstyle="{dynamicresource productlistitemcontainerstyle}" x:name=mylistbox > <i:interaction.triggers> <i:eventtrigger eventname="selectionchanged"> <i:invokecommandaction command="{binding selectionchangedcommand}" /> </i:eventtrigger> </i:interaction.triggers> </listbox>
when click same item again selectionchanged
event not firing.
so added button inside datatemplate this
<datatemplate x:key="productitemtemplate"> <button command="{binding datacontext.selectionchangedcommand,elementname=mylistbox}"> <stackpanel width="240"> <grid width="auto" height="auto" verticalalignment="stretch" background="white"> <image source="/assets/productcardbg.png" width="auto" height="auto" /> <image source="{binding thumbimage}" width="207" height="216" /> </grid> <textblock text="{binding name}" margin="0,5,0,0" style="{staticresource swmregulartextblockstyle}" fontsize="{staticresource verysmallfontsize}" /> <textblock text="{binding description}" style="{staticresource swmlighttextblockstyle}" fontsize="{staticresource smallfontsize}" /> <textblock text="{binding price}" style="{staticresource swmregulartextblockstyle}" fontsize="{staticresource smallfontsize}" /> </stackpanel> </button> </datatemplate>
but problem whenever select item command not executed. need click outside of stackpanel command getting executed. need execute command whenever click item.
please me, in advance.
if understand correctly can achieve using mouseclick event:
<i:interaction.triggers> <i:eventtrigger eventname="selectionchanged"> <i:invokecommandaction command="{binding mouseclick}" commandparameter="{binding elementname=mylistbox ,path=selecteditem}" /> </i:eventtrigger> </i:interaction.triggers>
and in code behind:
void mylistbox_mylistbox(object sender, mouseeventargs e) { //your code }
if want use command , not code behind that:
create class called basecommand:
public class basecommand : icommand { protected func<object, bool> _canexecute; protected action<object> _execute; public basecommand() { } public basecommand(func<object, bool> canexcecute, action<object> execute) { _canexecute = canexcecute; _execute = execute; } public bool canexecute(object parameter) { return _canexecute(parameter); } public void execute(object parameter) { _execute(parameter); } public event eventhandler canexecutechanged; } public bool canexecute(object parameter) { return _canexecute(parameter); } public void execute(object parameter) { _execute(parameter); }
in viewmodel:
private basecommand _mouseclick; public basecommand mouseclick { { if (null == _mouseclick) { _mouseclick = new basecommand(canexecutecommand, opendocument); } return _open; } } private bool canexecutecommand(object obj) { return true; //for example } public void mouseclickcommand(object o) { //do }
this way every time you'll select item it'll activate command.
Comments
Post a Comment