本文介绍了如何通过ControlTemplate中的触发器设置DropShadowEffect的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Button ControlTemplate,我正在尝试使用Trigger修改Border上的DropShadowEffect.这是我的Xaml:

I have a Button ControlTemplate and I'm trying to modify a DropShadowEffect on a Border by using a Trigger. Here is my Xaml:

<Button.Template>
  <ControlTemplate TargetType="Button">
    <Border x:Name="ButtonBorder" Margin="10" CornerRadius="5" Background="Gray">
      <Border.Effect>
        <DropShadowEffect ShadowDepth="5" x:Name="BorderEffect" />
      </Border.Effect>
      <ContentPresenter HorizontalAlignment="Center" />
    </Border>
    <ControlTemplate.Triggers>
      <Trigger Property="Button.IsMouseOver" Value="True">
        <Setter Property="Background" TargetName="ButtonBorder" Value="LightGray" />
      </Trigger>
      <Trigger Property="Button.IsPressed" Value="True">
        <Setter Property="Margin" TargetName="ButtonBorder" Value="13,13,7,7" />
        <!-- this is where I get the error -->
        <Setter Property="ShadowDepth" TargetName="BorderEffect" Value="2" />
      </Trigger>
    </ControlTemplate.Triggers>
  </ControlTemplate>
</Button.Template>

我遇到错误,指出找不到BorderEffect.
我也尝试过:

I get on error stating that the BorderEffect cannot be found.
I have also tried:

<Setter Property="Effect.ShadowDepth" TargetName="ButtonBorder" Value="2" />

但是我也收到一条错误消息,告诉我在类型Effect的对象上找不到属性ShadowDepth(因为它使用基类而不是DropShadowEffect)

But I also get an error telling me that the property ShadowDepth could not be found on the object of type Effect (because it's using the base class instead of DropShadowEffect)

我该如何解决?

推荐答案

您可以在setter.value中整体设置效果.

You can set the Effect as a whole in the setter.value

 <Setter Property="Margin" TargetName="ButtonBorder" Value="13,13,7,7" />
    <Setter Property="Effect" TargetName="ButtonBorder" >
      <Setter.Value>
         <DropShadowEffect ShadowDepth="2" />
      </Setter.Value>
    </Setter>
  </Trigger>

这篇关于如何通过ControlTemplate中的触发器设置DropShadowEffect的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 15:13