如何创建一个类似的图像 [英] How to create a comparable Image

查看:146
本文介绍了如何创建一个类似的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DataGridView的已被绑定到一个通用的的BindingList 。我希望能够适用的排序的和的搜索的有关类型的列 DataGridViewImageColumn 的基本思路是,以一个名字存储到图像标签和用途是用于排序和搜索。的我怎么能这样做?

I have a DataGridView which has been bound to a generic BindingList. I want to be able to apply sort and search on columns of type DataGridViewImageColumn. The basic idea is to store a name into the image Tag and use is for sorting and searching. How can I do that?

这似乎几种方式做到这一点:

It seems several ways to do it:


  1. 创建一个新的类继承为System.Drawing.Image 并使其可比性。

    • 图片是一个抽象类,如果我从它继承(以及 IComparable的接口),我将与此错误消息遇到:类型为System.Drawing.Image没有定义构造函数。 这里有什么问题吗?图像是摘要不是密封类,但它不会让我继承了!的继承

  1. Creating a new class inheriting System.Drawing.Image and making it comparable.
    • Image is an abstract class and if I inherit from it (as well as IComparable interface), I'll encounter with this error message: The type 'System.Drawing.Image' has no constructors defined. What's the problem here? Image is an abstract not a sealed class, but it doesn't allow me to inherit it!

使用保护覆盖 ApplySortCore 方法从类的BindingList< T>

Using protected override ApplySortCore method of inherited class from BindingList<T>.


  • 这方法是像这样

  • This method is like so:

class MyBindingList<T> : BindingList<T>
{
    ...
    protected override void ApplySortCore(PropertyDescriptor prop,
                                          ListSortDirection direction)
    {
        if (prop.PropertyType.Equals(typeof(Image)))
        {
            /* I have no idea! */
        }
    }
}



  • 这似乎并不容易,而且可以使用,如果其他的想法是不可用的。

在此先感谢

推荐答案

我找到了办法!

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;
    }
}






表格

class MyForm : Form
{
    ...

    void BindDGV()
    {
        dataGridView1.Columns["myColumnName"].DataPropertyName = "MyProp";
        dataGridView1.DataSource = MyBindingList<MyDataObject>(...);
    }
}

这篇关于如何创建一个类似的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆