c# - How to set a property of the parent element, when firing a trigger on ListBoxItem in WPF -
i have resourcedictionary
has style
responsible creating style of vertical side menu. below xaml resourcedictionary
of hypothetical example created demonstrate problem:
<resourcedictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:control="clr-namespace:firstfloor.modernui.presentation"> <style targettype="control:controle"> <setter property="template"> <setter.value> <controltemplate targettype="control:controle"> <grid> <border background="{templatebinding backcolor}"> <listbox x:name="linklist" itemssource="{binding links,relativesource={relativesource templatedparent}}" background="transparent"> <listbox.itemtemplate> <datatemplate> <grid height="50" width="500" > <textblock text="{binding displayname}" foreground="black" margin="45,2,2,2" fontsize="{dynamicresource mediumfontsize}" texttrimming="characterellipsis" verticalalignment="center" horizontalalignment="left" /> </grid> <datatemplate.triggers> <trigger property="ismouseover" value="true"> <trigger.setters> <setter property="control:controle.backcolor" value="red"/> </trigger.setters> </trigger> </datatemplate.triggers> </datatemplate> </listbox.itemtemplate> </listbox> </border> </grid> </controltemplate> </setter.value> </setter> </style>
the style based on class called controle
inherits control
. below code of class:
using system.windows; using system.windows.controls; using system.windows.media; namespace firstfloor.modernui.presentation { public class controle:control { /// <summary> /// identifies links dependency property. /// </summary> public static readonly dependencyproperty linksproperty = dependencyproperty.register("links", typeof(linkcollection), typeof(controle), new propertymetadata(new linkcollection())); /// <summary> /// identifies links dependency property. /// </summary> public static readonly dependencyproperty backcolorproperty = dependencyproperty.register("backcolor", typeof(solidcolorbrush), typeof(controle), new propertymetadata( new solidcolorbrush(color.fromrgb(0, 200,0)))); /// <summary> /// gets or sets collection of links define available content in tab. /// </summary> /// public linkcollection links { { return (linkcollection)getvalue(linksproperty); } set { setvalue(linksproperty, value); } } /// <summary> /// gets or sets collection of links define available content in tab. /// </summary> public solidcolorbrush backcolor { { return (solidcolorbrush)getvalue(backcolorproperty); } set { setvalue(backcolorproperty, value); } } } }
i wanna know why in line below:
<setter property="control:controle.backcolor" value="red"/>
i not able set property of controle
... funny thing if set ban ownership of other place, seem take place, when i'm inside itemtemplate setting has no effect.
imho best can without wiring procedural code is:
<controltemplate.triggers> <trigger property="ismouseover" value="true" sourcename="linklist"> <trigger.setters> <setter property="backcolor" value="red" /> </trigger.setters> </trigger> </controltemplate.triggers>
Comments
Post a Comment