如何创建可比较的图像

发布于 2024-12-08 15:43:51 字数 1125 浏览 0 评论 0 原文

我有一个DataGridView,它已绑定到通用BindingList。我希望能够对 DataGridViewImageColumn 类型的列应用排序搜索基本思想是将名称存储到图像标签中,并用于排序和搜索。我该如何做到这一点?

似乎有几种方法可以做到这一点:

  1. 创建一个继承 System.Drawing.Image 的新类并使其具有可比性。
    • Image 是一个抽象类,如果我继承它(以及 IComparable 接口),我会遇到以下错误消息:The type 'System. Drawing.Image' 没有定义构造函数。 这里有什么问题吗? Image 是一个抽象,而不是一个密封类,但它不允许我继承它!
  2. 使用受保护的覆盖BindingList 继承类的 >ApplySortCore 方法。

    • 这个方法是这样的:

      class MyBindingList; :BindingList
      {
          ...
          protected override void ApplySortCore(PropertyDescriptor prop,
                                                ListSortDirection方向)
          {
              if (prop.PropertyType.Equals(typeof(Image)))
              {
                  /* 我不知道! */
              }
          }
      }
      
  3. 创建一个继承自 DataGridViewImageColumn 的新 DataGridViewColumn
    • 这似乎并不容易,如果其他想法无法使用,可以使用。

提前致谢

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. 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!
  2. 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! */
              }
          }
      }
      
  3. Creating a new DataGridViewColumn inheriting from DataGridViewImageColumn.
    • This doesn't seem to be easy and may be used if other ideas are unusable.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无声静候 2024-12-15 15:43:51

创建一个封装 System.Drawing 的类 (X)。 Image + ImageAlias 字符串属性。将您的图像列绑定到 X.Image 并通过 X.ImageAlias 进行搜索。

抱歉,我手上没有编码工具来提供示例,但这是一个基本想法。

希望这有帮助。

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.

所有深爱都是秘密 2024-12-15 15:43:51

我找到路了!


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>(...);
    }
}

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>(...);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文