ZuTa
ZuTa

Reputation: 1454

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>

Upvotes: 93

Views: 81531

Answers (4)

L Chougrani
L Chougrani

Reputation: 726

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}}">

Upvotes: 51

Chris W.
Chris W.

Reputation: 23290

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."

Upvotes: 43

AJ Richardson
AJ Richardson

Reputation: 6830

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.

Upvotes: 25

Ungouge
Ungouge

Reputation: 1

If I understand correctly you are looking for OverridesDefaultStyle

<Style TargetType="{x:Type TextBox}">
      <Setter Property="OverridesDefaultStyle" Value="False" />
      <Setter Property="Margin" Value="2" />
      <Setter Property="Padding" Value="2" />
</Style>

Upvotes: -3

Related Questions