How to create a comparable Image

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-06 13:19:17

Create a class (X) that incapsulates System.Drawing. Image + ImageAlias string property. Bind your image column to X.Image and search over X.ImageAlias.

Sorry but don't have a coding tool on my hands to provide an example, but this is a basic idea.

Hope this helps.

I found the way!


MyBindingList

class MyBindingList<T> : BindingList<T>
{
    ...
    protected override void ApplySortCore(PropertyDescriptor prop,
                                          ListSortDirection direction)
    {
        if (prop.PropertyType.Equals(typeof(Image)))
        {
            _SortPropertyCore = prop;
            _SortDirectionCore = direction;

            var items = this.Items;
            Func<T, object> func =
                new Func<T, object>(t => (prop.GetValue(t) as Image).Tag);

            switch (direction)
            {
                case ListSortDirection.Ascending:
                    items = items.OrderBy(func).ToList();
                    break;

                case ListSortDirection.Descending:
                    items = items.OrderByDescending(func).ToList();
                    break;
            }

            ResetItems(items as List<T>);
            ResetBindings();
        }
        else
        {
            ...
        }
    }

    private void ResetItems(List<T> items)
    {
        base.ClearItems();

        for (int itemIndex = 0; itemIndex < items.Count; itemIndex++)
        {
            base.InsertItem(itemIndex, items[itemIndex]);
        }
    }
}

MyDataObject

class MyDataObject : INotifyPropertyChanged
{
    ...

    public Image MyProp
    {
        get
        {
            return CreateComparableImage(myImage, "myImage");
        }
    }

    private Image CreateComparableImage(Image image, string alias)
    {
        Image taggedImage = new Bitmap(image);
        taggedImage.Tag = alias;

        return taggedImage;
    }
}

Form

class MyForm : Form
{
    ...

    void BindDGV()
    {
        dataGridView1.Columns["myColumnName"].DataPropertyName = "MyProp";
        dataGridView1.DataSource = MyBindingList<MyDataObject>(...);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!