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

问题描述

我有一个 Button ControlTemplate 并且我正在尝试通过使用修改 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)

我该如何解决这个问题?

How can I solve this?

推荐答案

可以在setter.value中整体设置Effect

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 的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-07 04:23