Wednesday, 15 May 2013

c# - Variables in XAML Style -



c# - Variables in XAML Style -

i have next style:

<style x:key="buttonbase" targettype="button"> <setter property="background" value="#ff007bff"/> <setter property="foreground" value="white"/> <setter property="template"> <setter.value> <controltemplate targettype="button"> <controltemplate.triggers> <trigger property="ispressed" value="> <setter property="background" value="yellow" /> <setter property="foreground" value="black" /> </trigger> <trigger property="ispressed" value="false"> <setter property="background" value="#ff007bff" /> <setter property="foreground" value="white" /> </trigger> </controltemplate.triggers> </controltemplate> <setter.value> </setter> </style>

and inherited style:

<style x:key="elementbutton" targettype="button" basedon="{staticresource buttonbase}"> <setter property="margin" value="10"/> <setter property="height" value="200"/> </style>

what i'd able set arbitrary variable in base of operations style:

<setter variable="hovercolor" value="pink"/>

then i'd able utilize triggers such:

<trigger property="ispressed" value="> <setter property="background" value="{templatebinding hovercolor}" /> <setter property="foreground" value="black" /> </trigger>

and finally, override in inherited style:

<style x:key="elementbutton" targettype="button" basedon="{staticresource buttonbase}"> <setter property="margin" value="10"/> <setter property="height" value="200"/> <setter variable="hovercolor" value="red"/> </style>

is there way accomplish this? i've looked @ static resources these can't overridden. also, cannot utilize requires code-behind because don't have one!

its question , i've fought through sort of thing well. there may kind of xaml-only approach work, have feeling if there is, sense pretty kludgy. have couple of suggestions of how accomplish want.

first, quick observation. "don't have code-behind" , view "xaml-only". well, i've never seen usercontrol view doesn't have any code-behind file @ least, i'm assuming mean don't want set code in there (other obligatory initializecomponent()). having said that, approaches i'll outline won't require code in code-behind files.

at end of day, sounds want define custom "variables". these suggestions that, albeit maybe not in way envisioned doing so.

the first approach solve problem subclass control interested in styling , add together custom dependency properties it. example, subclass button, buttonwithmyvariables. 1 of custom dependency properties called "hovercolor", of type color, or perhaps more appropriately, "hoverbrush" of type brush (if you're wanting apply straight background or foreground property). base of operations style can set hovercolor whatever wants, , inherited style can override it, or can override straight on element in xaml. i'm not providing code samples approach (right now, unless requested) since more commonly-used approach i'm guessing you're familiar with.

the second approach define custom attached property. i've not seen approach used much dealing strictly styling issues, perhaps because attached "behavior" job in case, authors have used style files react (bind to) , apply visual changes based on attached property, rather code in attached property changed callback doing stylistically (but suppose still done way). however, approach feels "lighter weight" many, since don't need subclass existing controls.

an illustration of sec approach can found in mahapps.metro library, textboxhelper class (which houses attached properties) , controls.textbox.xaml style file (which binds attached properties). example, see in command template textbox, line makes utilize of watermark attached property:

<textblock x:name="message" text="{templatebinding controls:textboxhelper.watermark}" visibility="collapsed" foreground="{templatebinding foreground}" ishittestvisible="false" opacity="0.6" horizontalalignment="left" verticalalignment="{templatebinding verticalcontentalignment}" margin="6,0,0,0"/>

now can imagine set watermark value in base of operations style something:

<setter property="controls:textboxhelper.watermark" value="my helpful watermark all!"/>

and override in inherited style:

<setter property="controls:textboxhelper.watermark" value="a more specific watermark!"/>

with either approach, can define "variable" want , set them in style setter, override them in inherited styles, templatebind them within command templates, or trigger off of them.

c# wpf xaml

No comments:

Post a Comment