Wpf ListBox – change default selected-item style *inside* the ContentPresenter -
i have listbox in each item stackpanel. stackpanel consist of image , textblock below it:
<listbox.itemtemplate> <datatemplate> <stackpanel margin="10"> <image> <image.source> <bitmapimage urisource="{binding path=imagefilepath}"/> </image.source> </image> <textblock text="title" textalignment="center"/> </stackpanel> </datatemplate> </listbox.itemtemplate>
it looks this:
when user select item, default blue rectangle surround stackpanel:
now, want make different border selected-item, want surround image.
i know how make control template , put custom border around contentpresenter, this, of course, surround whole stackpanel, not image.
i don’t know if making changes contentpresenter possible, , if idea @ all. if there other way achieve want, fine well.
right, listbox's own contentpresenter isn't helpful you're doing. want a) eliminate listbox's own selection visuals , b) replace them more suitable in datatemplate items.
the default selection visual applied default template listboxitem
. replace template. using style in resources listbox
, apply own control template listboxitem
. not it, present content , don't provide selection background. handle selection visuals trigger in data template, image , label defined , can apply changes 1 , not other. below example works me.
note there's fiddling horizontalalignment on border element make cling image element within it. also, wrote quickie test viewmodel items property called items
; assume not name of collection member you're using populate own listbox.
<listbox margin="8" itemssource="{binding items}" > <listbox.resources> <style targettype="{x:type listboxitem}"> <setter property="horizontalcontentalignment" value="stretch" /> <setter property="template"> <setter.value> <controltemplate targettype="{x:type listboxitem}"> <grid> <contentpresenter /> </grid> </controltemplate> </setter.value> </setter> </style> </listbox.resources> <listbox.itemtemplate> <datatemplate> <grid> <border x:name="highlightborder" borderthickness="4" horizontalalignment="left" verticalalignment="top" margin="10" > <border.style> <style targettype="border"> <!-- must set default borderbrush via style, if set @ all. attribute on border tag, override effects of trigger below. --> <setter property="borderbrush" value="transparent" /> </style> </border.style> <image source="{binding imagefilepath}" /> </border> </grid> <datatemplate.triggers> <datatrigger binding="{binding isselected, relativesource={relativesource ancestortype=listboxitem}}" value="true"> <setter targetname="highlightborder" property="borderbrush" value="orange" /> </datatrigger> </datatemplate.triggers> </datatemplate> </listbox.itemtemplate> </listbox>
Comments
Post a Comment