【问题标题】:How to change templatized controls using styles如何使用样式更改模板化控件
【发布时间】:2010-08-21 15:18:47
【问题描述】:

我使用ControlTemplate 来定义我的按钮在 WPF 应用程序中的外观。此外,我想使用样式来设置按钮的某些方面。这些样式应该在ControlTemplate 中定义的元素上设置属性,例如(简化):

<Window.Resources>
    <ControlTemplate x:Key="Template1" TargetType="Button">
        <Grid>
            <Rectangle Name="rect" Fill="White" Stroke="Blue" StrokeThickness="2"/>
            <TextBlock Name="text" Text="Hallo" Foreground="Red" VerticalAlignment="Center" HorizontalAlignment="Center"/>
        </Grid>
    </ControlTemplate>
    <Style x:Key="Style1" TargetType="Button" >
        <Setter TargetName="rect" Property="Fill" Value="Red"/>
    </Style>
</Window.Resources>

现在编译器抱怨 TargetName“rect”不是我能理解的有效目标,因为未模板化的 Button 不包含名为“rect”的元素。

我知道我可以将样式更改为设置完整的模板,但我想避免这种情况(因为模板比此处显示的要复杂得多,我不想为每种样式复制它...)

是否有可能实现这种行为?也许通过设置TargetType 对吗?还有其他想法吗?

【问题讨论】:

    标签: wpf templates styles


    【解决方案1】:

    您可以仅覆盖控件模板的一部分。您可以更改所有内容,也可以不更改任何内容。

    不过,您可以为样式的属性设置设置器。

    【讨论】:

      【解决方案2】:

      标准模式是在控件模板中使用 TemplateBinding 绑定到控件本身的属性,然后在样式中设置控件的属性。例如:

      <Window.Resources>
          <ControlTemplate x:Key="Template1" TargetType="Button">
              <Grid>
                  <Rectangle Name="rect" Fill="{TemplateBinding Background}" Stroke="Blue" StrokeThickness="2"/>
                  <TextBlock Name="text" Text="Hallo" Foreground="Red" VerticalAlignment="Center" HorizontalAlignment="Center"/>
              </Grid>
          </ControlTemplate>
          <Style x:Key="Style1" TargetType="Button" >
              <Setter Property="Background" Value="Red"/>
          </Style>
      </Window.Resources>
      

      这会将 rect 上的 Fill 属性绑定到 Button 上的 Background 属性。该样式会将背景属性设置为红色,这将导致填充设置为红色。

      为了设置默认值,您通常会创建一个样式来设置模板以及其他属性:

      <Window.Resources>
          <Style x:Key="BaseStyle" TargetType="Button">
              <Setter Property="Template">
                  <Setter.Value>
                      <ControlTemplate TargetType="Button">
                          <Grid>
                              <Rectangle Name="rect" Fill="{TemplateBinding Background}" Stroke="Blue" StrokeThickness="2"/>
                              <TextBlock Name="text" Text="Hallo" Foreground="Red" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                          </Grid>
                      </ControlTemplate>
                  </Setter.Value>
              </Setter>
              <Setter Property="Background" Value="White"/>
          </Style>
          <Style x:Key="Style1" TargetType="Button" BasedOn="{StaticResource BaseStyle}">
              <Setter Property="Background" Value="Red"/>
          </Style>
      </Window.Resources>
      

      第一个样式将应用模板并将背景设置为白色,因此矩形将是白色的。第二种样式继承自第一种样式,但将背景设置为红色,因此矩形将是红色的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-09-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-13
        • 1970-01-01
        • 2016-10-16
        相关资源
        最近更新 更多