dataGridView绑定到列表-转换bool到图像



我有一个绑定到我的dataGridView项目的BindingList。Item类是这样的;

public class Item : INotifyPropertyChanged
{
    private string _Name;
    private bool _Active;
    public event PropertyChangedEventHandler PropertyChanged;
    public string Name
    {
        get { return _Name; }
        set {
            _Name = value;
            this.NotifyPropertyChanged("Name");
        }
    }
    public bool Active
    {
        get { return _Active; }
        set {
            _Active = value;
            this.NotifyPropertyChanged("Active");
        }
    }
    private void NotifyPropertyChanged(string name)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(name));
    }
}

然后是Bindinglist &dataGridView;

BindingList<Item> ItemList = new BindingList<Item>();
dataGridView1.DataSource = ItemList;

我希望bool Active在dataGridView上显示为Checked图像,当它为true时,否则不显示任何内容。dataGridView顶部的按钮允许用户将一行标记为Active。

当前dataGridView显示一个复选框。我怎么能有一个正确的绑定从一个bool在项目对象在dataGridView的图像?

修复了它,我改变了项目类来保存图像,而不是试图转换绑定中的bool值;

    public Image CheckImage
    {
        get
        {
            if (Active)
                return Properties.Resources.check;
            else
                return null;
        }
    }

最新更新