Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a style based on default style?

How to create a style based on default style in Silverlight?

For example, in WPF we make it like:

<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
  <Setter Property="Margin" Value="2" />
  <Setter Property="Padding" Value="2" />
</Style>
like image 613
ZuTa Avatar asked Oct 22 '12 17:10

ZuTa


3 Answers

Pretty much the same. Just minus the x:Type with more explicit naming.

<Style TargetType="TextBox" BasedOn="{StaticResource DefaultTextBoxStyle}">

More information here in the docs. PS, in case you need the default templates, TextBox for example would normally be found in CoreStyles.xaml

ADDENDUM as requested in the comments in case you're confused at the first read of the answer;

"you DO need a base style, which is really easy to do as you're meant to do it in an application theme like silverlight provides by default (wpf/uwp etc won't have these) that creates the files like ToolkitStyles.xaml, SDKStyles.xaml, CoreStyles.xaml, etc... Which is WHERE the staticresource name in the answer came from as targeting a silverlight version from the year this was originally answered."

like image 113
Chris W. Avatar answered Nov 20 '22 14:11

Chris W.


For Silverlight only:

To create a style based on the default style, you need to create a named style, then make the default style based on the named style (http://weblogs.asp.net/lduveau/silverlight-how-to-inherit-from-an-implicit-style)

<Style x:Key="DefaultCustomControlStyle" TargetType="local:CustomControl">
    <Setter Property="Padding" Value="2" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:CustomControl">
                <ContentPresenter />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<Style TargetType="local:CustomControl" BasedOn="{StaticResource DefaultCustomControlStyle}" />

If you're using WPF, it's much simpler to use the code in the original question instead.

like image 43
AJ Richardson Avatar answered Nov 20 '22 14:11

AJ Richardson


I would recommand to have a look at : https://justinmchase.com/2009/05/29/derived-styles-based-on-unnamed-default-styles/ It would go like this for you :

<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
like image 29
L Chougrani Avatar answered Nov 20 '22 14:11

L Chougrani