在数据绑定场景中,我应该如何设置 ImageListBox 项的图像索引?
我有一个文档对象,如下所示:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 WPF 中,我会使用 MVVM 模式来解决该问题:XPO 对象不会直接由 UI 使用,而是 ViewModel 对象将公开必要的属性,以便可以轻松地在绑定场景中使用它们。 MVVM 是 WPF 特有的,但我相信 MVP 模式非常相似,并且可以轻松地在 Windows 窗体中使用。因此,您可以创建一个 Presenter 对象,它将充当 UI 和 XPO 对象之间的适配器:
现在您只需将
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 :
Now you just have to set the
DataSource
to a collection ofDocumentPresenter
objects, and set theImageIndexMember
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 ;)