c# - 我的逻辑在这里需要一点帮助。我怎样才能让图片在点击时选择更改图像?

标签 c# wpf algorithm

这是我所拥有的:

private void HeroMouseEnter(object sender, MouseEventArgs e)
    {            
        //I did things this was because it is easier to maintain code in a sense that is is a generic
        //method made for all of the heroes images.
        ((Image)sender).Source = GetGlowingImage(((Image)sender).Name);            
    }

    private void HeroMouseLeave(object sender, MouseEventArgs e)
    {
        //I did this IF conditional because I want to ask, "If the sender object
        //is the hero selected, then do NOT change the image to normal.
        if (SelectedHero != ((Image)sender).Name)
        {
            //I did things this was because it is easier to maintain code in a sense that is is a generic
            //method made for all of the heroes images.
            ((Image)sender).Source = GetNormalImage(((Image)sender).Name);                             
        }            
    }

    private void HeroMouseClick(object sender, MouseEventArgs e)
    {
        if (!HasSelectedAHeroBefore)
        {
            HasSelectedAHeroBefore = true;
            //Created a generic way to play the announcers voice according to where a user clicked.
            string soundfile = "AnnouncerVoice/" + ((Image)sender).Name + ".mp3";
            soundPlayer.Open(new Uri(soundfile, UriKind.Relative));
            soundPlayer.Play();

            //I call the MouseEnter event in order to have the clicked picture glow and set the Selected bool to true 
            //to keep it highlighted.
            SelectedHero = ((Image)sender).Name; 
        }
        else if (HasSelectedAHeroBefore)
        {
            //Created a generic way to play the announcers voice according to where a user clicked.
            string soundfile = "AnnouncerVoice/" + ((Image)sender).Name + ".mp3";
            soundPlayer.Open(new Uri(soundfile, UriKind.Relative));
            soundPlayer.Play();

            //I call the MouseEnter event in order to have the clicked picture glow and set the Selected bool to true 
            //to keep it highlighted.
            SelectedHero = ((Image)sender).Name;
            PreviousSelectedHero = ((Image)sender).Name;
        }

    }

我想要的悬崖笔记。 当用户在我的图片上移动鼠标时,我希望图片发光。我通过在 MouseEnter 上将图像更改为经过 Photoshop 处理的图像(带有发光)来实现此目的。在 MouseLeave 上,我将图片切换回正常。

当用户点击时,我希望被点击的图片与我制作的 Glow 图片保持一致。一直以来,当用户移动他的鼠标时,我仍然希望它们在 MouseEnter 时发光并在 MouseLeave 时消光。

最后,如果用户点击了与所选图片不同的图片,则点击的图片必须像之前的图片一样保持选中状态(发光)。

我很困惑,我确信这很容易解决,我只是有点生疏。

非常感谢您的帮助。 :D

编辑:抱歉,我忘了说什么不起作用。一切都与我想要的完全一致,但是当我点击另一张图片时,前一张图片会一直发光(就像选中的一样),直到我将鼠标放在它上面并离开它。

Edit2:我添加了一些可能有用的东西。但我不知道如何通过名称选择图像控件。有什么帮助吗?

else if (HasSelectedAHeroBefore)
        {
            //Created a generic way to play the announcers voice according to where a user clicked.
            string soundfile = "AnnouncerVoice/" + ((Image)sender).Name + ".mp3";
            soundPlayer.Open(new Uri(soundfile, UriKind.Relative));
            soundPlayer.Play();

            //I call the MouseEnter event in order to have the clicked picture glow and set the Selected bool to true 
            //to keep it highlighted.
            PreviousSelectedHero = SelectedHero;
            //Here I want to select the Image control by it's Name property. But it says I can't convert string to Image. ANy help?

            GetNormalImage(((Image)PreviousSelectedHero).Name);
            SelectedHero = ((Image)sender).Name;

        }

最佳答案

作为创建多个图像的替代方法,您可以使用 Triggers 和 BitMapEffects 来完成大部分此类行为。有个好tutorial here使图像在鼠标悬停时具有发光效果。只要图像是“选定”图像,您就可以使用类似的方法和 DataTrigger 来保持发光效果。

在鼠标悬停时“发光”图像的示例样式设置:

  <Style.Triggers>
    <Trigger Property="IsMouseOver" Value="True">
      <Setter Property="BitmapEffect">
        <Setter.Value>
          <OuterGlowBitmapEffect GlowColor="Red" GlowSize="4"/>
        </Setter.Value>
      </Setter>
    </Trigger>
  </Style.Triggers>

无论鼠标悬停如何,您都可以如何使用类似的方法使所选图像保持发光:

<Style.Triggers>
    <DataTrigger Binding={Binding Path=IsSelected} Value="True">
      <Setter Property="BitmapEffect">
        <Setter.Value>
          <OuterGlowBitmapEffect GlowColor="Red" GlowSize="4"/>
        </Setter.Value>
      </Setter>
    </DataTrigger>
</Style.Triggers>

关于c# - 我的逻辑在这里需要一点帮助。我怎样才能让图片在点击时选择更改图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1928606/

相关文章:

c# - 获取 Oracle 匿名 block 中更新或删除的记录

c# - 更改按钮控件模板,现在鼠标悬停不起作用

wpf - 如何单向绑定(bind)到 materialDesign :ButtonProgressAssist. IsIndicatorVisible DependencyProperty

c# - 使用 ClickOnce 安装我的应用程序 - 缺少 dll 消息(尽管未请求)

algorithm - 渐近分析比较 f 和 g

c# - 如何以特定格式绑定(bind)日期时间列中的网格

c# - 如何在 WPF 中创建 TableLayoutPanel?

c# - 当通用决策基于数据库值时,如何使用 C# 泛型来实现此实现

algorithm - 未知大小数组的二进制搜索

algorithm - 如何确保在三消游戏关卡中目标进球不是不可能的?