gpt4 book ai didi

c# - 具有效果和触发器的图像模板

转载 作者:太空宇宙 更新时间:2023-11-03 22:02:11 24 4
gpt4 key购买 nike

您好,我有如下所示的 wpf xaml 代码

<Image Source="/MyProject;component/Resources/Icons/button_cancel_256.png"
DockPanel.Dock="Right"
Margin="0,1,10,1"
RenderTransformOrigin="0.5,0.5"
>
<Image.RenderTransform>
<ScaleTransform x:Name="ImageScaleTransform"
ScaleX="1" ScaleY="1"
/>
</Image.RenderTransform>
<Image.Effect>
<DropShadowEffect x:Name="ImageGlowEffect"
BlurRadius="20"
ShadowDepth="0"
Color="White">
</DropShadowEffect>
</Image.Effect>
<Image.Triggers>
<EventTrigger RoutedEvent="Image.MouseEnter">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetName="ImageGlowEffect"
Storyboard.TargetProperty="Color"
From="White"
To="Red"
Duration="0:0:1">
</ColorAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
<EventTrigger RoutedEvent="Image.MouseLeave">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetName="ImageGlowEffect"
Storyboard.TargetProperty="Color"
From="Red"
To="White"
Duration="0:0:1">
</ColorAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
<EventTrigger RoutedEvent="Image.MouseLeftButtonDown">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="ImageScaleTransform"
Storyboard.TargetProperty="ScaleX"
From="1"
To=".9"
Duration="0:0:0.1">
</DoubleAnimation>
<DoubleAnimation
Storyboard.TargetName="ImageScaleTransform"
Storyboard.TargetProperty="ScaleY"
From="1"
To=".9"
Duration="0:0:0.1">
</DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>

<EventTrigger RoutedEvent="Image.MouseLeftButtonUp">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="ImageScaleTransform"
Storyboard.TargetProperty="ScaleX"
From=".9"
To="1"
Duration="0:0:0.1">
</DoubleAnimation>
<DoubleAnimation
Storyboard.TargetName="ImageScaleTransform"
Storyboard.TargetProperty="ScaleY"
From=".9"
To="1"
Duration="0:0:0.1">
</DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Image.Triggers>

现在我想创建一个样式或模板,以便我可以将其应用到我可以指定颜色的其他图像。

请帮助我,经过各种尝试后,我仍然无法做到这一点。

最佳答案

添加到 EvAlex 发布的内容中,您可以动态更改特定于图像的颜色,然后使用 DynamicResourceOpacity 双动画(代替 ColorAnimation),如下所示。 .

  <Style TargetType="{x:Type Image}"
x:Key="DropShadowImageStyle">
<Setter Property="RenderTransform">
<Setter.Value>
<ScaleTransform
ScaleX="1"
ScaleY="1" />
</Setter.Value>
</Setter>
<Setter Property="Effect">
<Setter.Value>
<DropShadowEffect
BlurRadius="20"
ShadowDepth="0"
Opacity="0"
Color="{DynamicResource MyToColor}">
</DropShadowEffect>
</Setter.Value>
</Setter>
<Style.Triggers>
<EventTrigger RoutedEvent="Image.MouseEnter">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetProperty="Effect.Opacity"
From="0"
To="1"
Duration="0:0:1">
</DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
<EventTrigger RoutedEvent="Image.MouseLeave">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetProperty="Effect.Opacity"
From="1"
To="0"
Duration="0:0:1">
</DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
<EventTrigger RoutedEvent="Image.MouseLeftButtonDown">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetProperty="RenderTransform.ScaleX"
From="1"
To=".9"
Duration="0:0:0.1">
</DoubleAnimation>
<DoubleAnimation
Storyboard.TargetProperty="RenderTransform.ScaleY"
From="1"
To=".9"
Duration="0:0:0.1">
</DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>

<EventTrigger RoutedEvent="Image.MouseLeftButtonUp">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetProperty="RenderTransform.ScaleX"
From=".9"
To="1"
Duration="0:0:0.1">
</DoubleAnimation>
<DoubleAnimation
Storyboard.TargetProperty="RenderTransform.ScaleY"
From=".9"
To="1"
Duration="0:0:0.1">
</DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Style.Triggers>
</Style>

<StackPanel Orientation="Horizontal">
<Image Source="MyImage1.JPG"
Width="50" Height="50" Margin="5"
Style="{StaticResource DropShadowImageStyle}">
<Image.Resources>
<Color x:Key="MyToColor">Red</Color>
</Image.Resources>
</Image>
<Image Source="MyImage2.JPG"
Width="50" Height="50" Margin="5"
Style="{StaticResource DropShadowImageStyle}">
<Image.Resources>
<Color x:Key="MyToColor">Blue</Color>
</Image.Resources>
</Image>
<Image Source="MyImage3.JPG"
Width="50" Height="50" Margin="5"
Style="{StaticResource DropShadowImageStyle}">
<Image.Resources>
<Color x:Key="MyToColor">Orange</Color>
</Image.Resources>
</Image>
</StackPanel>

关于c# - 具有效果和触发器的图像模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9696519/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com