c# - WPF Adding a custom property in a control -
ok have wpf application in have resource dictionary. in dictionary have new style button
looks :
<style x:key="menubuttonstyle" targettype="button"> <setter property="template"> <setter.value> <controltemplate targettype="{x:type button}"> <grid> <image x:name="imager" renderoptions.bitmapscalingmode="highquality" width="{templatebinding width}" height="{templatebinding height}" source="{binding relativesource={relativesource templatedparent}, path=tag}" verticalalignment="center" horizontalalignment="center" stretch="uniformtofill"/> </grid> <controltemplate.triggers> <trigger property="ismouseover" value="true"> <setter targetname="imager" property="width" value="24" /> <setter targetname="imager" property="height" value="24" /> </trigger> <trigger property="ispressed" value="true"> <setter targetname="imager" property="width" value="16" /> <setter targetname="imager" property="height" value="16" /> </trigger> </controltemplate.triggers> </controltemplate> </setter.value> </setter> </style>
and use stye in xaml :
<button x:name="home" style="{dynamicresource menubuttonstyle}" width="20" height="20" tag="\images\home.png"/> <button x:name="settings" style="{dynamicresource menubuttonstyle}" width="20" height="20" tag="\images\settings.png"/>
now want is, create new property in button
style named overwidth
, overheight
in order use :
<trigger property="ismouseover" value="true"> <setter targetname="imager" property="width" value= overwidth/> <setter targetname="imager" property="height" value= overheight/> </trigger>
and in xaml :
<button x:name="home" style="{dynamicresource menubuttonstyle}" width="20" height="20" tag="\images\home.png" overwidth = "24"/>
i dont know if possible. dont want use binding someintenger
from c# code. in advance.
you can use attached properties:
public class extensions { public static readonly dependencyproperty overwidthproperty = dependencyproperty.registerattached("overwidth", typeof(double), typeof(extensions), new propertymetadata(default(double))); public static void setoverwidth(uielement element, double value) { element.setvalue(overwidthproperty, value); } public static double getoverwidth(uielement element) { return (double)element.getvalue(overwidthproperty); } }
xaml:
xmlns:extensions="clr-namespace:namespace extensions class" <trigger property="ismouseover" value="true"> <setter property="minwidth" value="{binding path=(extensions:extensions.overwidth), relativesource={relativesource self}}"/> </trigger> <button extensions:extensions.overwidth="100" width="10"/>
Comments
Post a Comment