Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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>

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

I don't understand why this was accepted as an answer. It clearly is based on a named style and not on the default (and unnamed) style. – Nestor Oct 6, 2016 at 8:49 @ChrisW. No, I just pointed out that DefaultTextBoxStyle is a name of a given style and there is no solution to use an unnamed, control-based style as a base. – Nestor Oct 6, 2016 at 14:27

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

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.

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>
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.