Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

wpf - How can I set a property of a DropShadowEffect via a Trigger in a ControlTemplate?

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>

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

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...