Custom button template in WPF -
i want create simple button template image , text inside it. want keep system button's , feel.
how create it, step step?
p.s.: have tried customcontrol
in wpf , basedon
property.
you can style , attached property:
<resourcedictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ap="clr-namespace:myproject.namespace.path.to.buttonproperties"> ... <style x:key="imagebutton" targettype="button"> <setter property="contenttemplate"> <setter.value> <datatemplate> <stackpanel orientation="horizontal"> <image source="{binding path=(ap:buttonproperties.image), relativesource={relativesource findancestor, ancestortype={x:type button}}}"></image> <contentpresenter content="{binding path=content, relativesource={relativesource findancestor, ancestortype={x:type button}}}"></contentpresenter> </stackpanel> </datatemplate> </setter.value> </setter> </style> ... </resourcedictionary>
and
public class buttonproperties { public static imagesource getimage(dependencyobject obj) { return (imagesource)obj.getvalue(imageproperty); } public static void setimage(dependencyobject obj, imagesource value) { obj.setvalue(imageproperty, value); } public static readonly dependencyproperty imageproperty = dependencyproperty.registerattached("image", typeof(imagesource), typeof(buttonproperties), new uipropertymetadata((imagesource)null)); }
then in markup:
<button style="{staticresource imagebutton}" ap:buttonproperties.image="{staticresource myimage}" content="test"> </button>
this example looks pretty hideous, can change stackpanel
grid
or similar constrain image proportion. using contentpresenter
allows preserve behaviour of button allowing put uielement
inside, , retaining command support etc.
Comments
Post a Comment