3

我是 WPF 的新手。我不知道该怎么做。

我定义了这种风格 -

<Style TargetType="{x:Type Button}" x:Key="StandardButton">

<Setter Property="Background" Value="{StaticResource LightBackground}"/>

    <Setter Property="Foreground" Value="{StaticResource Foreground}"/>

</Style>

我有一个控制模板 -

<ControlTemplate x:Key="ExpanderTemplate" TargetType="{x:Type Expander}">

<ControlTemplate.Resources>

        <Style TargetType="{x:Type Button}" /* Here I need to put above defined style */></Style> 

    </ControlTemplate.Resources>

</ControlTemplate>
4

2 回答 2

1

如果您希望所有人都Buttons使用ControlTemplateStyle只需x:Key从样式中删除并添加到ControlTemplate.Resources

<ControlTemplate.Resources>
    <Style TargetType="{x:Type Button}">
        <Setter Property="Background" Value="{StaticResource LightBackground}"/>
        <Setter Property="Foreground" Value="{StaticResource Foreground}"/>
    </Style>
</ControlTemplate.Resources>

Styleswith an x:Keywill 必须在控件上声明Style="{StaticResource StandardButton}",以应用于范围内的所有控件,Resources您只需声明TargetType

如果您已经Style在更高级别中定义了一个Resources并且您想应用到ButtonsControlTemplate可以使用该BasedOn属性的所有内容。

例子:

<ControlTemplate x:Key="ExpanderTemplate" TargetType="{x:Type Expander}">
   <ControlTemplate.Resources>
      <Style TargetType="{x:Type Button}" BasedOn="{StaticResource StandardButton}" />
   </ControlTemplate.Resources>
</ControlTemplate>

这将适用StandardButton Style于它定义的范围内的所有按钮Resources,在这种情况下,所有ButtonsExpanderTemplate

于 2013-02-18T07:12:11.160 回答
0

您可以使用 StaticResource 和您定义的资源的键名来引用您的样式

Style="{StaticResource StandardButton}"
于 2013-02-18T06:47:21.173 回答