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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13016932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 16:40:50  来源:igfitidea点击:

How to create a style based on default style?

.netwpfxamlsilverlight

提问by ZuTa

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

如何根据 Silverlight 中的默认样式创建样式?

For example, in WPF we make it like:

例如,在 WPF 中,我们将其设置为:

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

采纳答案by Chris W.

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

基本上一样。只需减去x:Type更明确的命名。

<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

文档中的更多信息。PS,如果您需要默认模板,例如 TextBox 通常会在 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."

确实需要一个基本样式,这真的很容易做到,因为您打算在 Silverlight 等默认提供的应用程序主题中执行此操作(wpf/uwp 等不会有这些),它会创建类似 ToolkitStyles.xaml 的文件、SDKStyles.xaml、CoreStyles.xaml 等......答案中的静态资源名称来自哪里,因为它针对的是最初回答这一年的 Silverlight 版本。”

回答by AJ Richardson

For Silverlight only:

仅适用于 Silverlight

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)

要基于默认样式创建样式,您需要创建一个命名样式,然后根据命名样式创建默认样式(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}" />

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

如果您使用的是 WPF,则改用原始问题中的代码要简单得多。

回答by Ungouge

If I understand correctly you are looking for OverridesDefaultStyle

如果我理解正确,您正在寻找 OverridesDefaultStyle

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