c# - How to change itemsControl stackpanel background color -
says have list of 10 items somelist
, show them on page via itemscontrol
below:
<itemscontrol datacontext="{binding [someviewmodel]}" borderbrush="black" itemsource="{binding somelist}"> <itemscontrol.itemtemplate> <datatemplate> <border borderthickness="1" background="green"> <stackpanel mousedown="{binding path=datacontext.somecommand, relativesource={relativesource findancestor,ancestortype={x:type itemscontrol}}}" command parameter="{binding someid}"> <textblock text="{binding something}"> </stackpanel> </border> </datatemplate> </itemscontrol.itemtemplate> </itemscontrol>
i able trigger somecommand
method , able pass in someid
input parameter. i'm wondering how update stackpanel background color, making looks "selected". meaning item have green background, when click on 1 of stackpanel, stackpanel should change background red , change others green
if want use itemscontrol
can change itemtemplate
radiobutton
custom controltemplate
include border
background
change red when ischecked == true
:
<itemscontrol datacontext="{binding [someviewmodel]}" borderbrush="black" itemsource="{binding somelist}"> <itemscontrol.itemtemplate> <datatemplate> <radiobutton content="{binding something}" groupname="radiogroup"> <radiobutton.template> <controltemplate targettype="{x:type radiobutton}"> <border background="green" x:name="part_border"> <contentpresenter/> </border> <controltemplate.triggers> <trigger property="ischecked" value="true"> <setter targetname="part_border" property="background" value="red"/> </trigger> </controltemplate.triggers> </controltemplate> </radiobutton.template> </radiobutton> </datatemplate> </itemscontrol.itemtemplate> </itemscontrol>
however don't see reason why cannot use listbox
selectionmode=single
(default value) , change template
of listboxitem
:
<listbox datacontext="{binding [someviewmodel]}" borderbrush="black" itemsource="{binding somelist}"> <listbox.itemcontainerstyle> <style targettype="{x:type listboxitem}"> <setter property="template"> <setter.value> <controltemplate targettype="{x:type listboxitem}"> <border background="green" x:name="part_border"> <contentpresenter/> </border> <controltemplate.triggers> <trigger property="isselected" value="true"> <setter targetname="part_border" property="background" value="red"/> </trigger> </controltemplate.triggers> </controltemplate> </setter.value> </setter> </style> </listbox.itemcontainerstyle> </listbox>
or this, without changing template
:
<listbox datacontext="{binding [someviewmodel]}" borderbrush="black" itemsource="{binding somelist}"> <listbox.resources> <solidcolorbrush x:key="{x:static systemcolors.highlightbrushkey}" color="red"/> </listbox.resources> <listbox.itemcontainerstyle> <style targettype="{x:type listboxitem}"> <setter property="background" value="green"/> </style> </listbox.itemcontainerstyle> </listbox>
in wpf it's easier pick control has functionality need , style want other way round
Comments
Post a Comment