dataGridView绑定到List - 将bool转换为Image

时间:2015-05-16 16:36:38

标签: c# winforms data-binding datagridview

我有一个绑定到我的dataGridView的Items的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图像,否则不显示。 dataGridView顶部的按钮允许用户将行标记为Active。

目前dataGridView显示一个复选框。如何从项目对象中的bool到dataGridView中的图像正确绑定?

1 个答案:

答案 0 :(得分:3)

修正了它,我改变了item类来保存图像,而不是试图在绑定中翻译bool;

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