单击按钮后如何更改图像源? [英] How to change an image source after clicking a button?

查看:86
本文介绍了单击按钮后如何更改图像源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用图片作为按钮。因此,默认情况下我需要图像源为 /image1.png ,当我点击图像时,如果 $ c>功能并将其图像源更改为 /image2.png 。我正确地更改了图像,问题是我必须在第一次单击时单击两次图像才能更改。



这就是我正在使用的内容:

  public MainWindow()
{
InitializeComponent();
IsPlaying = false;
//PlayBtn.Source =(ImageSource)new ImageSourceConverter()。ConvertFrom(@C:\ Users \ myusername \Documents\Visual Studio 2013 \Projects\Project1 \ Project1WPF \ image1 .PNG);
}

private void PlayBtn_MouseDown(object sender,MouseButtonEventArgs e)
{
if(IsPlaying == false)
{

PlayBtn.Source =(ImageSource)new ImageSourceConverter()。ConvertFrom(@C:\ Users \ myusername \Documents\Visual Studio 2013 \Projects \ Project1 \ Project1WPF \ image.png) ;
IsPlaying = true;
} else if(IsPlaying == true)
{

PlayBtn.Source =(ImageSource)new ImageSourceConverter()。ConvertFrom(@C:\ Users \ myusername \Documents\Visual Studio 2013 \Projects \ Project1 \ Project1WPF \ image.png);
IsPlaying = false;
}


解决方案

要解决您的问题,只需正确设置初始状态。按照目前的情况,你有这个:


  1. 图像以image1开头。

  2. 构造函数集 isPlaying false

  3. 单击按钮

  4. 处理程序运行,因为 isPlaying false ,图像设置为image1 (您的第一个区块)

  5. isPlaying 设置为 true

  6. 您再次单击该按钮

  7. 处理程序再次运行,因为 isPlaying true 图像设置为image2。

因此要么翻转哪个图像设置为当前值为 false ,或将初始值设置为 true 以获取您描述的行为。



顺便说一句,你可能根本不应该在代码隐藏中执行此操作。 Source 属性应绑定到View Model(通过转换器),按钮的 Command 更改该源。 / p>

I'm using anImage as a button. So I need the image source to be /image1.png by default, and when I click the Image, it will make an if function and change its image source to /image2.png. I changes the image correctly, the problem is that I have to click two times the image to change when it is first clicked.

This is what I'm using:

public MainWindow()
    {
        InitializeComponent();
        IsPlaying = false;
        //PlayBtn.Source = (ImageSource)new ImageSourceConverter().ConvertFrom(@"C:\Users\myusername\Documents\Visual Studio 2013\Projects\Project1\Project1WPF\image1.png");
    }

private void PlayBtn_MouseDown(object sender, MouseButtonEventArgs e)
    {
        if(IsPlaying == false)
        {

            PlayBtn.Source = (ImageSource)new ImageSourceConverter().ConvertFrom(@"C:\Users\myusername\Documents\Visual Studio 2013\Projects\Project1\Project1WPF\image1.png");
            IsPlaying = true;
        }else if(IsPlaying == true)
        {

            PlayBtn.Source = (ImageSource)new ImageSourceConverter().ConvertFrom(@"C:\Users\myusername\Documents\Visual Studio 2013\Projects\Project1\Project1WPF\image2.png");
            IsPlaying = false;
        }       

解决方案

To fix your problem, simply set the initial state correctly. As it stands, you have this:

  1. The image starts with "image1".
  2. The constructor sets isPlaying to false
  3. You click the button
  4. The handler runs, since isPlaying is false, the image is set to "image1" (your first block)
  5. isPlaying is set to true
  6. You click the button again
  7. The handler runs again, since isPlaying is true the image is set to "image2".

So either flip which image is set when the current value is false, or set the initial value to true to get the behavior you describe.

As an aside, you probably shouldn't be doing this in the code-behind at all. The Source property should be bound to your View Model (through a converter) and the button's Command changes that source.

这篇关于单击按钮后如何更改图像源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆