listbox - Wpf Close popup when ListboxItem is selected -
i have listbox inside popup. how close popup right after select item list box; here code:
<popup x:name="colorpopup" allowstransparency="true" isopen="{binding elementname=colortoggle, path=ischecked}" placement="bottom" staysopen="false" placementtarget="{binding elementname=colortoggle}"> <border x:name="dropdownborder1" borderbrush="{dynamicresource {x:static systemcolors.windowframebrushkey}}" margin="0, 0,5,5" borderthickness="1" background="{dynamicresource {x:static systemcolors.windowbrushkey}}" effect="{dynamicresource windowshadoweffect}"> <listbox name="colorlist" verticalcontentalignment="stretch" margin="1, 3, 1, 3" isenabled="true" grid.column="0" background="transparent" horizontalcontentalignment="center" selecteditem="{binding fcolor}" selectionmode="single" style="{staticresource horizontallistboxstyle2}" itemssource="{binding fillcolors}"> </listbox> </border> </popup>
subscribe selectionchanged event.
you can either in code-behind:
private void listbox_selectionchanged_1(object sender, selectionchangedeventargs e) { // colorpopup.isopen = false; ?? or colortoggle.ischecked = false; }
<listbox selectionchanged="listbox_selectionchanged_1" ... />
or maybe if use mvvm pattern...
it example mvvm-light:
<listbox ...> <i:interaction.triggers> <i:eventtrigger eventname="selectionchanged"> <cmd:eventtocommand command="{binding path=closepopupcommand}" passeventargstocommand="true" /> </i:eventtrigger> </i:interaction.triggers> </listbox>
or prism same :
<listbox> <i:interaction.triggers> <i:eventtrigger eventname="selectionchanged"> <prism:invokecommandaction command="{binding path=closepupupcommand}" /> </i:eventtrigger> </i:interaction.triggers> </listbox>
or if have dirty mind close popup in fcolor
property setter. :)
public object fcolor { { return this.fcolorfield; } set { this.fcolorfield= value; iscolortoggelchecked = false; raisepropertychanged(() => this.fcolor); } }