仅在 WPF/XAML 中悬停图像时的投影效果或模糊效果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12021131/
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 05:00:59  来源:igfitidea点击:

Drop Shadow Effect or blur effect only on Image on-hovering in WPF/XAML

c#wpfvisual-studio-2010xaml

提问by now he who must not be named.

I have an image with a shadow property defined as follows.

我有一个阴影属性定义如下的图像。

 <Border Background="Black" Margin="42,180,368,38">
   <Border.Effect>
     <DropShadowEffect Color="Aqua"  Opacity="0.5"/>
   </Border.Effect>

   <Image Height="92" Width="97" Source="/Images/image_search.png" />
 </Border>

This produces image with a border. OK. But, I need the border to be shown only on image-hovering? How do I do that ? Also, how to blur the image on-hovering upon it?

这会产生带边框的图像。好的。但是,我只需要在图像悬停时显示边框?我怎么做 ?另外,如何模糊悬停在其上的图像?

Many Thanks.

非常感谢。

回答by Batuu

You can do this with styles and triggers.

您可以使用样式和触发器来做到这一点。

    <Border Margin="42,180,368,38"> 
        <Border.Style>
            <Style TargetType="Border">
                <Setter Property="Background" Value="{x:Null}"/>
                <Setter Property="BorderThickness" Value="0"/>
                  <Style.Triggers>
                    <Trigger Property="Border.IsMouseOver" Value="True">
                        <Setter Property="Background" Value="Black"/>                            
                        <Setter Property="Effect">
                            <Setter.Value>
                                <DropShadowEffect Color="Aqua"  Opacity="0.5"/>
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Border.Style>            
        <Image Height="92" Width="97" Source="/Images/image_search.png" />
    </Border>    

The first Setter within the Style explicitly sets the background color to null and the border thickness to '0' to "hide" the border. The Trigger sets the borders background property when the IsMouseOver Property of the border control is set to true.

样式中的第一个 Setter 将背景颜色显式设置为 null,将边框粗细设置为“0”以“隐藏”边框。当边框控件的 IsMouseOver 属性设置为 true 时,触发器设置边框背景属性。