0

我有一个充满多个图像的 WPF 窗口,我试图找到一种方法来模糊鼠标未悬停的所有图像。

我正在使用此事件来影响我悬停的图像:

private void Window_MouseMove(object sender, MouseEventArgs e)
{
    if (e.OriginalSource is Image)
    {
        ((Image)e.OriginalSource).Effect = new BlurEffect();
    }
}

这就是我创建图像的方式

        StackPanel imgStkpnl = new StackPanel();

        foreach (var urll in Tools.MyGlobals.GoogleImageURLs)
        {
            var image = new System.Windows.Controls.Image();
            var fullFilePath = urll;

            BitmapImage bitmap = new BitmapImage();
            bitmap.BeginInit();
            try
            {
                bitmap.UriSource = new Uri(fullFilePath, UriKind.Absolute);
                bitmap.EndInit();

                image.Source = bitmap;
                image.Stretch = Stretch.Uniform;
                image.HorizontalAlignment = HorizontalAlignment.Left;

                imgStkpnl.Children.Add(image);
            }
            catch { }
        }

如果有人知道影响所有其他图像的方法,这将是一个很大的帮助,我是一个大新手,所以对我放轻松:) 谢谢

4

1 回答 1

1

我能够使用此 XAML 达到您想要的效果:

<Window x:Class="Solutions.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel>

        <StackPanel.Resources>
            <BlurEffect x:Key="BlurEffect"></BlurEffect>

            <Style TargetType="Image">
                <Setter Property="Effect" Value="{StaticResource BlurEffect}"></Setter>

                <Style.Triggers>
                    <EventTrigger RoutedEvent="MouseEnter" >
                        <BeginStoryboard>
                            <Storyboard>
                                <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Effect">
                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Null}"></DiscreteObjectKeyFrame>
                                </ObjectAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                    <EventTrigger RoutedEvent="MouseLeave">
                        <BeginStoryboard>
                            <Storyboard>
                                <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Effect">
                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource BlurEffect}"></DiscreteObjectKeyFrame>
                                </ObjectAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </Style.Triggers>
            </Style>
        </StackPanel.Resources>

        <Image Source="cat.jpg" Width="200"></Image>
        <Image Source="cat.jpg" Width="200"></Image>
        <Image Source="cat.jpg" Width="200"></Image>
    </StackPanel>
</Window>

例子

根据您的评论,要使图像不模糊,只需删除MouseLeave事件触发器

于 2018-08-04T13:05:44.597 回答