c# - Extracting same properites to style in WPF XAML -
i'm stuck extracting properties element's style in xaml file. have many repeated blocks like:
<controls:roundabletoggleradiobutton style="{staticresource roundabletoggleradiobuttonstyle}"> <stackpanel> <image width="32" margin="2" source="images/inbox_upload.png" /> <textblock margin="2" foreground="white" text="extract" textalignment="center" /> </stackpanel>
so want extract same properties every button style , able change image , text. this:
<setter property="contenttemplate"> <setter.value> <datatemplate> <stackpanel> <image width="32" margin="2" /> <textblock margin="2" foreground="white" textalignment="center" /> </stackpanel> </datatemplate> </setter.value>
<controls:roundabletoggleradiobutton style="{staticresource roundabletoggleradiobuttonstyle}"> <stackpanel> <image source="images/inbox_upload.png" /> <textblock text="extract"/> </stackpanel>
so possible @ all? or there workarounds? help)))
create new class inherits button
public class imagetextbutton : button { public static readonly dependencyproperty iconproperty = dependencyproperty.register("icon", typeof (imagesource), typeof (imagetextbutton), null); public static readonly dependencyproperty textproperty = dependencyproperty.register("text", typeof (string), typeof (imagetextbutton), null); public imagetextbutton() { this.defaultstylekey = typeof(imagetextbutton); } public imagesource icon { { return (imagesource) getvalue(iconproperty); } set { setvalue(iconproperty, value); } } public string text { { return (string) getvalue(textproperty); } set { setvalue(textproperty, value); } } }
xaml
<window x:class="wpfapplication3.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:wpfapplication3="clr-namespace:wpfapplication3" title="mainwindow" height="350" width="525"> <window.resources> <style targettype="{x:type wpfapplication3:imagetextbutton}"> <setter property="template"> <setter.value> <controltemplate targettype="{x:type wpfapplication3:imagetextbutton}"> <stackpanel height="auto" orientation="horizontal"> <image source="{templatebinding icon}" stretch="fill"/> <textblock text="{templatebinding text}"/> </stackpanel> </controltemplate> </setter.value> </setter> </style> </window.resources> <grid> <wpfapplication3:imagetextbutton text="submit" icon="hydrangeas.jpg"></wpfapplication3:imagetextbutton> </grid>
instead of creating new text property can use content property of button.
Comments
Post a Comment