单击按钮后如何更改图像源? [英] How to change an image source after clicking a button?
问题描述
我使用图片
作为按钮。因此,默认情况下我需要图像源为 /image1.png
,当我点击图像时,如果$ c,它将生成 $ 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;
}
要解决您的问题,只需正确设置初始状态。按照目前的情况,你有这个:
- 图像以image1开头。
- 构造函数集
isPlaying
到false
- 单击按钮
- 处理程序运行,因为
isPlaying
是false
,图像设置为image1 (您的第一个区块) -
isPlaying
设置为true
- 您再次单击该按钮
- 处理程序再次运行,因为
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:
- The image starts with "image1".
- The constructor sets
isPlaying
tofalse
- You click the button
- The handler runs, since
isPlaying
isfalse
, the image is set to "image1" (your first block) isPlaying
is set totrue
- You click the button again
- The handler runs again, since
isPlaying
istrue
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屋!