下面做了一个例子,如下图,这个例子的主要目的是,把业务模型实体类映射到具体的UI控件上
上面在Module(自己创建的业务工程)右键添加一个实体类模型 Attachment
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SmartClient.Module { public class Attachment { public event EventHandler StatusChanged; public enum AttachmentStatus { NotAvailable, Downloading, AvailableNotModified, AvailableModified, ToBeUploaded, Uploading, Uploaded } private string _displayName; public string DisplayName { get { return _displayName; } set { _displayName = value; } } private string _fileName; public string FileName { get { return _fileName; } set { _fileName = value; } } private string _documentUrl; public string DocumentUrl { get { return _documentUrl; } set { _documentUrl = value; } } private AttachmentStatus _status; public AttachmentStatus Status { get { return _status; } set { _status = value; } } private DateTime _localCreationTime; public DateTime LocalCreationTime { get { return _localCreationTime; } set { _localCreationTime = value; } } } }
再添加一个映射器类,AttachmentMapper,它的作用是把这个业务实体绑定到ListView中
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; namespace SmartClient.Module { public static class AttachmentMapper { public static ListViewItem ToListViewItem(Attachment attachment) { return new AttachmentListViewItem(attachment); } public static Attachment FromListViewItem(ListViewItem listViewItem) { AttachmentListViewItem attachmentListViewItem = listViewItem as AttachmentListViewItem; return attachmentListViewItem.Attachment; } private class AttachmentListViewItem : ListViewItem { private Attachment _attachment; public AttachmentListViewItem(Attachment attachment) { Name = attachment.DisplayName; Text = attachment.DisplayName; SubItems.Add(AttachmentStatusToDisplayText(attachment)); _attachment = attachment; } private static string AttachmentStatusToDisplayText(Attachment attachment) { switch (attachment.Status) { case Attachment.AttachmentStatus.AvailableModified: return "Modified"; case Attachment.AttachmentStatus.AvailableNotModified: return "Available"; case Attachment.AttachmentStatus.Downloading: return "Downloading..."; case Attachment.AttachmentStatus.NotAvailable: return "To be downloaded"; case Attachment.AttachmentStatus.ToBeUploaded: return "To be uploaded"; case Attachment.AttachmentStatus.Uploaded: return "Uploaded"; case Attachment.AttachmentStatus.Uploading: return "Uploading"; } return attachment.Status.ToString(); } public Attachment Attachment { get { return _attachment; } } } } }
然后我们在View中,拖放一个ListView上去,并命名为_attachmentsListView,并修改其.cs代码
using System; using System.Windows.Forms; using Microsoft.Practices.CompositeUI.SmartParts; using Microsoft.Practices.ObjectBuilder; using SmartClient.Infrastructure.Interface; namespace SmartClient.Module { public partial class View : UserControl, IView { public View() { InitializeComponent(); } protected override void OnLoad(EventArgs e) { _presenter.OnViewReady(); base.OnLoad(e); //加入如下代码,在窗体加载时执行对ListView的绑定 var attachment = new Attachment(); attachment.DisplayName = "这是一个测试"; AddAttachmentToList(attachment); } private void AddAttachmentToList(Attachment attachment) { ListViewItem item = AttachmentMapper.ToListViewItem(attachment); _attachmentsListView.Items.Add(item); } } }
最后我们得到如下的界面,ListView已经绑定上我们测试的数据