如何根据默认样式创建样式?

ZuT*_*uTa 67 .net silverlight wpf xaml

如何在Silverlight中基于默认样式创建样式?

例如,在WPF中我们使它像:

<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
  <Setter Property="Margin" Value="2" />
  <Setter Property="Padding" Value="2" />
</Style>
Run Code Online (Sandbox Code Playgroud)

Chr*_* W. 26

几乎相同的.只需减去x:Type更明确的命名.

<Style TargetType="TextBox" BasedOn="{StaticResource DefaultTextBoxStyle}">
Run Code Online (Sandbox Code Playgroud)

更多信息请参见文档.PS,如果您需要默认模板,例如TextBox通常可以在CoreStyles.xaml中找到

如果您在第一次阅读答案时感到困惑,请按照评论中的要求添加附件;

" 你需要一个基本风格,这很容易做,因为你打算在一个应用程序主题中执行它,比如Silverlight默认提供(wpf/uwp等不会有这些),它们会创建像ToolkitStyles.xaml这样的文件,SDKStyles.xaml,CoreStyles.xaml等......答案中的静态资源名称来自于最初回答的那一年中针对Silverlight版本的问题."

  • 我不明白为什么这被接受为答案.它显然基于**命名样式**而不是默认(和_unnamed_)样式. (17认同)
  • @ChrisW.不,我只是指出_DefaultTextBoxStyle_是给定样式的名称,并且**没有解决方案**使用未命名的,基于控件的样式作为基础. (4认同)
  • 但如果我想继承默认风格的自定义控件(由我开发)? (2认同)
  • 不,我有自定义控件的默认样式.我想以它的新风格为基础. (2认同)
  • 这个答案是对的.它假设已经有一个命名样式来基于新的. (2认同)

AJ *_*son 21

要基于默认样式创建样式,您需要创建命名样式,然后根据命名样式创建默认样式(http://weblogs.asp.net/lduveau/silverlight-how-to-inherit-from - 隐式式)

<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}" />
Run Code Online (Sandbox Code Playgroud)


小智 11

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}}">
Run Code Online (Sandbox Code Playgroud)