简体   繁体   English

如何控制浏览按钮的点击?

[英]How do I control the clicks of a browse button?

I have a browse button, and two empty images (image1, image2). 我有一个浏览按钮,以及两个空白图像(image1,image2)。 I want to first click the browse button and load an image to (image1). 我想先单击浏览按钮,然后将图像加载到(image1)。 On the second click I want to load the image to (image2). 在第二次单击上,我要将图像加载到(image2)。

I'm very new to WPF and C# in general. 一般来说,我对WPF和C#还是很陌生。 I guess I need some method to control the clicks of the button? 我想我需要一些方法来控制按钮的点击? Does anyone have any idea about this? 有人对此有任何想法吗? I would highly appreciate it. 我将不胜感激。

This is my code behind attempt: 这是我的尝试背后的代码:

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        OpenFileDialog fd = new OpenFileDialog();
        fd.DefaultExt = ".tif";
        fd.Filter = "(*.tif,*.tiff)|*.tif;*.tiff";
        fd.ShowDialog();
        string fname = fd.FileName;
        textBox1.Text = fname;

        image1.Source = new BitmapImage(new Uri(fd.FileName));
    }

After this, the first image is displayed in image1, but when I browse for another image it comes on top of image 1, and not in image2. 此后,第一个图像显示在image1中,但是当我浏览另一个图像时,它出现在图像1的顶部,而不是在image2中。 How can I make the second image that I browse display in image2? 如何使浏览的第二个图像显示在image2中? maybe something like, if the button is already clicked one time, then the image should load into image2? 也许是这样的,如果按钮已经被单击过一次,那么图像应该加载到image2中? or if image1 is already full then load to image2? 还是如果image1已满,然后加载到image2? I'm not sure! 我不确定!

Oh and the purpose of this is to create an interface that lets the user browse different images shown in a listbox, and when the user clicks each image, it displays in another window and the user can zoom in and out and so on. 哦,这样做的目的是创建一个界面,使用户可以浏览列表框中显示的不同图像,并且当用户单击每个图像时,它会显示在另一个窗口中,并且用户可以放大和缩小等等。

But right now I'm just stuck with this small part of my project! 但是现在,我只是停留在项目的这一小部分!

While I question the why you want to do such a thing, you could use the following. 当我质疑为什么要这样做时,可以使用以下内容。 Also, please show some effort next time. 另外,下次请付出一些努力。 This is a relatively easy solution! 这是一个相对简单的解决方案!

private bool _ImageOneSet;

public MainWindow()
{
    InitializeComponent();
    _ImageOneSet = false;
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    if (!_ImageOneSet)
    {
        // set image one
        _ImageOneSet = true;
    }
    else
    {
        // set image two
    }       
}

I use a private field that is set to false. 我使用一个设置为false的私有字段。 The first time the button_click event gets triggered, _ImageOneSet is still false , so you can set the first image. 第一次触发button_click事件时, _ImageOneSet仍为false ,因此您可以设置第一张图像。

The second (and third, fourth etc...), _ImageOneSet is true so you will set the second image. 第二个(以及第三个,第四个等等) _ImageOneSettrue因此您将设置第二个图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM