c# - Clicking a button in a listviewitem that is not selected and getting selected item -
i have question listview setup example below. when click button below in expander header want item selected well, i'm seeing while button command work, item selected still previous item selected, not item button in. how can have item selected when button clicked?
i tried setting controltemplate this, did not work.
<style targettype="{x:type listviewitem}"> <setter property="horizontalcontentalignment" value="stretch" /> <setter property="template"> <setter.value> <controltemplate targettype="{x:type listviewitem}"> <contentpresenter horizontalalignment="{templatebinding horizontalcontentalignment}" verticalalignment="{templatebinding verticalcontentalignment}" snapstodevicepixels="{templatebinding snapstodevicepixels}" /> <controltemplate.triggers> <trigger property="iskeyboardfocuswithin" value="true"> <setter property="isselected" value="true" /> </trigger> </controltemplate.triggers> </controltemplate> </setter.value> </setter> </style> <listview itemssource="{binding myitemsource, mode=twoway}" selecteditem="{binding myselecteditem, mode=twoway}"> <listview.itemtemplate> <datatemplate> <expander isexpanded="{binding relativesource={relativesource mode=findancestor, ancestortype={x:type listviewitem}}, path=isselected}"> <expander.header> <button command={binding mycommand}>click me</button> </expander.header> <!-- content here --> </expander> </datatemplate> </listview.itemtemplate> </listview>
i suggest defining command selectitem
in main viewmodel takes item selected parameter. execution method of command can set myselecteditem
property, set property isselected
on item viewmodel true
, invoke further actions on item (i.e. executed mycommand
). selection logic in viewmodel , clean binding don't need use listview
@ can stick plain itemscontrol
:
the xaml looks this:
<itemscontrol itemssource="{binding myitemsource}"> <itemscontrol.itemtemplate> <datatemplate> <expander isexpanded="{binding isselected}"> <expander.header> <button command="{binding relativesource={relativesource mode=findancestor, ancestortype=itemscontrol}, path=datacontext.selectitem}" commandparameter="{binding"}>click me</button> </expander.header> <!-- content here --> </expander> </datatemplate> </itemscontrol.itemtemplate> </itemscontrol>
the mainviewmodel this:
public class mainviewmodel : inotifypropertychanged { public observablecollection<itemviewmodel> myitemssource { get; private set; } public itemviewmodel selecteditem { get... set... } public icommand selectitem { get; private set; } ctor()... private void executeselectitem(itemviewmodel item) { selecteditem = item; foreach (var in myitemssource) i.isselected = false; item.isselected = true; item.dosomething(); } }
i have found way easier use itemscontrol
implement few lines of selection logic myself, instead of dealing messy binding of selection of listview
. in opinion quite intuitive implement custom selection behavior (multiple items, allowing combinations, etc.). can use isselected
property apply custom styling of selected items.