88

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>
Rekshino
  • 6,954
  • 2
  • 19
  • 44
ZuTa
  • 1,404
  • 1
  • 11
  • 19

4 Answers4

42

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

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Chris W.
  • 22,835
  • 3
  • 60
  • 94
  • 2
    but if I want inherit default style of custom control(developed by me)? – ZuTa Oct 22 '12 at 19:22
  • Oh, then just replace fields with your own. Like instead of DefaultTextBoxStyle replace with YourStyleTemplate, etc. Right? – Chris W. Oct 22 '12 at 19:41
  • 2
    No, I have a default style for my custom control. And I want to based on it new style. – ZuTa Oct 22 '12 at 20:56
  • 46
    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 06 '16 at 08:49
  • @Nestor I'm guessing you're thinking of WPF and requiring {x:Type Blah} as opposed to silverlight like the question is reference to? – Chris W. Oct 06 '16 at 14:25
  • 4
    @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 06 '16 at 14:27
  • @Nestor, yes you DO need a base style, which is really easy to do as you're meant to do it in an application theme that creates the files like ToolkitStyles.xaml, SDKStyles.xaml, CoreStyles.xaml, etc... Which is WHERE the name in the answer came from and WHY it was marked as correct. To say there is no solution is inaccurate if it can still be accomplished. – Chris W. Oct 06 '16 at 14:49
  • @ChrisW. I wasn't aware of the filename-based styles. So if I create a base style: `` and put it in `MyStyles.xaml`, and in another file I add this: ` – Nestor Oct 06 '16 at 17:03
  • @Nestor sorry no, by implicitly defining a TargetType it would effect every TextBox IF you had your mystyles.xaml as a MergedDict in your App.xaml, however assuming you don't then it would do nothing unless you provide it an x:Key to reference as a StaticResource on the instances you want to hit it from but you still need to make that resource dict visible to your app via app.xaml as merged dict. – Chris W. Oct 06 '16 at 22:14
  • @Nestor wait I read the last part wrong. If you have your resource dict mystyles.xaml visibile to the app as a merged dict, with a Key name, then in another file you could do `` and all children of wherever it's specified would inherit from the Style with the FontSize set. – Chris W. Oct 06 '16 at 22:16
  • 4
    This answer is wrong. It assumes there's already a named style to base the new one on. – disklosr Oct 26 '16 at 20:47
  • 1
    @ChrisW. I would add in the answer what you said in the comments: _"you DO need a base style, which is really easy to do as you're meant to do it in an application theme that creates the files like ToolkitStyles.xaml, SDKStyles.xaml, CoreStyles.xaml, etc... Which is WHERE the name in the answer came from"_. Really, you answer seemed wrong to me too, at first. It makes no sense to learn that it's right only through reading 10 comments... – Massimiliano Kraus Jun 09 '17 at 08:34
  • @MassimilianoKraus Done :) – Chris W. Jun 09 '17 at 14:18
  • 1
    @NathanKovner Sorry but I'll try to remember back since this question is nearing a decade old now. However if I recall in Silverlight the default styles are named, hence why targeting just the type like you would in WPF wasn't working for the OP. Also if I remember right when you go to edit a default style there's a name on it. A lot of the confusion I believe came from peoples lack of familiarity between WPF and Silveright nuances but personally I've not touched Silverlight in many years now either so I to am likely no longer the best resource to confirm nor deny your comment. Cheers – Chris W. Feb 21 '20 at 15:24
  • 1
    Hi Chris, I think you're right, a lot of the confusion with this question has to do with the differences between WPF and Silverlight, going to +1 this answer. – Nathan Kovner Feb 21 '20 at 16:28
39

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}}">
L Chougrani
  • 588
  • 4
  • 7
24

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.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
AJ Richardson
  • 6,610
  • 1
  • 49
  • 59
-3

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>
Ungouge
  • 1
  • 1
  • 2