在数据绑定场景中,我应该如何设置 ImageListBox 项的图像索引?

发布于 2024-08-06 14:25:42 字数 358 浏览 2 评论 0原文

我有一个文档对象,如下所示:

public class Document
{
    public Title { get; set; }
    public Extension { get; set; }
    public byte[] Data { get; set; }
}

扩展名是“pdf”、“doc”、“docx”等。该文档用于在数据库中存储文档(它实际上是一个 DevExpress XPO 对象)。

我遇到的问题是,我将这些对象的列表绑定到图像列表框,该图像列表框具有要为每种文件类型显示的图标的关联图像列表。如何根据扩展设置 imagelistbox 项上的图像索引,而不将索引存储在域对象中?

I have a Document object that looks like this:

public class Document
{
    public Title { get; set; }
    public Extension { get; set; }
    public byte[] Data { get; set; }
}

The Extension is "pdf", "doc", "docx" and the like. This document is used for storing documents in a database (it's actually a DevExpress XPO object).

The problem I'm having is, I am binding a list of these objects to an imagelistbox, which has an associated image list of the icons to display for each file type. How can I set the image index on the imagelistbox item based on the Extension without storing the index in the domain object?

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

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

发布评论

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

评论(1

飘过的浮云 2024-08-13 14:25:43

在 WPF 中,我会使用 MVVM 模式来解决该问题:XPO 对象不会直接由 UI 使用,而是 ViewModel 对象将公开必要的属性,以便可以轻松地在绑定场景中使用它们。 MVVM 是 WPF 特有的,但我相信 MVP 模式非常相似,并且可以轻松地在 Windows 窗体中使用。因此,您可以创建一个 Presenter 对象,它将充当 UI 和 XPO 对象之间的适配器:

public class DocumentPresenter
{
    private Document _document;

    public DocumentPresenter(Document document)
    {
        _document = document;
    }

    public string Title
    {
        get { return _document.Title; };
        set { _document.Title = value; };
    }

    public string Extension
    {
        get { return _document.Extension; };
        set { _document.Extension = value; };
    }

    public byte[] Data
    {
        get { return _document.Data; };
        set { _document.Data = value; };
    }

    public int ImageIndex
    {
        get
        {
            // some logic to return the image index...
        }
    }

}

现在您只需将 DataSource 设置为 DocumentPresenter 对象的集合,并将 ImageIndexMember 设置为“ImageIndex”

免责声明:我从未真正使用过 MVP 模式,只使用过 MVVM,所以我可能弄错了......无论如何,我猜你明白了;)

In WPF, I would have used the MVVM pattern to solve that issue : the XPO object wouldn't be directly used by the UI, instead a ViewModel object would expose the necessary properties so that they can easily be used in binding scenarios. MVVM is specific to WPF, but I believe the MVP pattern is very similar and can easily be used in Windows Forms. So, you could create a Presenter object which would act as an adapter between the UI and the XPO object :

public class DocumentPresenter
{
    private Document _document;

    public DocumentPresenter(Document document)
    {
        _document = document;
    }

    public string Title
    {
        get { return _document.Title; };
        set { _document.Title = value; };
    }

    public string Extension
    {
        get { return _document.Extension; };
        set { _document.Extension = value; };
    }

    public byte[] Data
    {
        get { return _document.Data; };
        set { _document.Data = value; };
    }

    public int ImageIndex
    {
        get
        {
            // some logic to return the image index...
        }
    }

}

Now you just have to set the DataSource to a collection of DocumentPresenter objects, and set the ImageIndexMember to "ImageIndex"

Disclaimer : I never actually used the MVP pattern, only MVVM, so I might have got it wrong... anyway, you get the picture I guess ;)

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