MouseEnter WPF 上的发光效果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18399401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:29:01  来源:igfitidea点击:

Glow effect on MouseEnter WPF

c#wpfxamltriggersdropshadow

提问by Babak.Abad

I'm new in WPF(c#). I need make a glow effect around image control using triggers. How can I do make glow effect on mouse-enterevent? I want to use your answer i my style.

我是 WPF(c#) 的新手。我需要使用triggers. 我怎样才能在mouse-enter事件上制作发光效果?我想用你的回答我的风格。

My effect is:

我的效果是:

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

I see many links but they don't work.

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

回答by dkozl

To add glow to Imagecontrol you need to set Effectto your DropShadowEffectwhen IsMouseOver=True, something like this:

要添加发光Image控制,您需要设置EffectDropShadowEffectwhen 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>

回答by Irfan

If you want to reuse your effect, you must capture IsMouseOver trigger and set Control.Effect property to what you have defined in your resources.

如果您想重用您的效果,您必须捕获 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>

for this, you must place you effect in recourses of current page/window/usercontrol

为此,您必须将效果置于当前页面/窗口/用户控件的资源中

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