MouseEnter WPF上的辉光效果

时间:2013-08-23 09:28:46

标签: c# wpf xaml triggers dropshadow

我是WPF的新手(c#)。我需要使用triggers围绕图像控制制作发光效果。如何在mouse-enter事件中制作发光效果? 我想用我的风格来回答你。

我的效果是:

<DropShadowEffect x:Key="MyEffect" ShadowDepth="0" Color="Blue" Opacity="1" BlurRadius="20"/>

我看到很多链接,但它们不起作用。

2 个答案:

答案 0 :(得分:14)

要向Image控件添加发光,您需要在Effect时将DropShadowEffect设置为IsMouseOver=True,如下所示:

<Image Source="/WpfApplication1;component/myimage.png">
   <Image.Style>
      <Style TargetType="{x:Type Image}">
         <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
               <Setter Property="Effect">
                  <Setter.Value>
                     <DropShadowEffect ShadowDepth="0" Color="Blue" Opacity="1" BlurRadius="20"/>
                  </Setter.Value>
               </Setter>
            </Trigger>
         </Style.Triggers>
      </Style>
   </Image.Style>
</Image>

答案 1 :(得分:8)

如果要重用效果,必须捕获IsMouseOver触发器并将Control.Effect属性设置为您在资源中定义的属性。

<Button Width="100" Content="Hello Glow" >
 <Button.Style>
  <Style>
   <Style.Triggers>
    <Trigger Property="Button.IsMouseOver" Value="True">
     <Setter Property="Button.Effect" Value="{StaticResource MyEffect}" />
    </Trigger>
   </Style.Triggers>
  </Style>
 </Button.Style>
</Button>

为此,您必须将效果放在当前页面/窗口/用户控件

的资源中
<Window.Resources>
 <DropShadowEffect x:Key="MyEffect" ShadowDepth="0" Color="Blue" Opacity="1" BlurRadius="20"/>
</Window.Resources>
相关问题