c#点击更改pictureBox DataBinding

时间:2017-08-22 21:44:20

标签: c# data-binding

我有一个pictureBox,我通过Visual Studio中的属性将DataBindings设置为我的表的特定图像类型字段。到目前为止,它带来了数据库的形象。

但是我需要这个pictureBox根据点击的按钮改变它带来的图像。我的桌子上有4个图像区域。如果用户单击button1,我需要将此pictureBox与我表的字段image1进行数据绑定。如果用户单击按钮2,请更改其数据并将图像保存在表格的image2字段中。

我该怎么做?

2 个答案:

答案 0 :(得分:0)

我会考虑引入一个图像处理程序,如下所述:https://www.dotnetperls.com/ashx

ImageHandler将用于确定将哪个图像返回给客户端应用程序。例如,如果单击按钮1,则返回图像1。

答案 1 :(得分:0)

你可以创建一个这样的类:

public class ImageViewModel : INotifyPropertyChaged
{

    public event PropertyChangedEventHandler PropertyChanged;

    private int state = 0;
    public int State 
    { 
        set
        {
            this.state = value;
            this.OnPropertyChanged("State");
            this.OnPropertyChanged("CurrentBitmap");
        }
        get
        {
            return this.state;
        }
    }
    public Bitmap SelectedBitmap
    {
        get
        {
            if(this.state = 0)
                return Bitmap1; //You have to add the logic for Bitmap1
            else if(this.state = 1)
                return Bitmap2; //You have to add the logic for Bitmap1
            else if(this.state = 2)
                return Bitmap3; //You have to add the logic for Bitmap1
            else if(this.state = 3)
                return Bitmap4; //You have to add the logic for Bitmap1
        }
    }

    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

然后你必须将你的PictureBox绑定在SelectedBitmap