c# - XAML Style Won't Apply Until EventTrigger Command Completes -
i have listbox defines custom controltemplate. selected item has style changes background , foreground, , style works. however, want introduce behaviour displays modal message box on selection changed, asking user if want select different item. i've implemented icommand in code below shown areyousurecommand.
the problem whilst modal message box shown, background style selected item changed foreground not. dismiss modal message box, foreground colour changes. haven't included code icommand because it's bit convoluted sufficient window opened showdialog when executed.
can shed light on why background colour changes not foreground colour?
<listbox x:name="submenu" itemssource="{binding myitems}"> <listbox.itemtemplate> <datatemplate> <textblock text="{binding path=displayname}" foreground="{binding foreground, relativesource={relativesource ancestortype=contentcontrol}}" /> </datatemplate> </listbox.itemtemplate> <listbox.resources> <style targettype="{x:type listboxitem}"> <setter property="template"> <setter.value> <controltemplate targettype="{x:type listboxitem}"> <border x:name="mainborder"> <contentcontrol x:name="presenter"> <contentpresenter /> </contentcontrol> </border> <controltemplate.triggers> <trigger property="isselected" value="true"> <!-- setter on mainborder applies before areyousurecommand completes --> <setter targetname="mainborder" property="background" value="red" /> <!-- setter on presenter applies after areyousurecommand completes --> <setter targetname="presenter" property="foreground" value="green" /> </trigger> </controltemplate.triggers> </controltemplate> </setter.value> </setter> </style> </listbox.resources> <i:interaction.triggers> <i:eventtrigger eventname="selectionchanged"> <i:invokecommandaction command="{binding areyousurecommand}" commandparameter="{binding selecteditem, elementname=submenu}" /> </i:eventtrigger> </i:interaction.triggers> </listbox>
i came solution this. use setter isselected
property push value of isselected
listviewitem
onto view model. use datatrigger
instead of regular trigger set selected styles, , bind trigger isselected
property on view model rather property of listviewitem
itself. don't know why works - shouldn't different really, work.
thanks juan , ben comments.
<listbox x:name="submenu" itemssource="{binding myitems}"> <listbox.itemtemplate> <datatemplate> <textblock text="{binding path=displayname}" foreground="{binding foreground, relativesource={relativesource ancestortype=contentcontrol}}" /> </datatemplate> </listbox.itemtemplate> <listbox.resources> <style targettype="{x:type listboxitem}"> <setter property="isselected" value="{binding isselected}" /> <setter property="template"> <setter.value> <controltemplate targettype="{x:type listboxitem}"> <border x:name="mainborder"> <contentcontrol x:name="presenter"> <contentpresenter /> </contentcontrol> </border> <controltemplate.triggers> <datatrigger binding="{binding isselected}" value="true"> <setter targetname="mainborder" property="background" value="red" /> <setter targetname="presenter" property="foreground" value="green" /> </datatrigger> </controltemplate.triggers> </controltemplate> </setter.value> </setter> </style> </listbox.resources> <i:interaction.triggers> <i:eventtrigger eventname="selectionchanged"> <i:invokecommandaction command="{binding areyousurecommand}" commandparameter="{binding selecteditem, elementname=submenu}" /> </i:eventtrigger> </i:interaction.triggers> </listbox>
Comments
Post a Comment