ListView数据绑定类
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace NClay.Windows
{
public delegate void EventDataListItemHandler<T>(object source,T e);
public abstract class DataListBase<T>
{
public event EventDataListItemHandler<T> ItemClick;
public event EventDataListItemHandler<T> ItemDoubleClick;
public event EventDataListItemHandler<T> ItemDataBound;
public DataListBase(ListView view)
{
mView = view;
Init();
}
public DataListBase(ListView view, EventDataListItemHandler<T> click,
EventDataListItemHandler<T> dbclick, EventDataListItemHandler<T> bound)
{
mView = view;
ItemClick += click;
ItemDoubleClick += dbclick;
ItemDataBound += bound;
Init();
}
private void Init()
{
mView.MouseDown += new MouseEventHandler(view_mousedown);
mView.MouseClick += new MouseEventHandler(view_mouseclick);
mView.MouseDoubleClick += new MouseEventHandler(view_mousedbclick);
mView.SelectedIndexChanged += new EventHandler(view_selectchange);
mView.Columns.Clear();
foreach (ColumnHeader item in Columns)
{
mView.Columns.Add(item);
}
}
private ListView mView;
private ListViewItem mSelectItem;
protected abstract ListView.ColumnHeaderCollection Columns
{
get;
}
protected abstract ListViewItem CreateItem(T e);
private IList<T> mDataSource;
public IList<T> DataSource
{
get
{
return mDataSource;
}
set
{
mDataSource = value;
OnDataBind();
}
}
private void OnDataBind()
{
mView.Items.Clear();
ListViewItem vitem;
foreach (T item in DataSource)
{
vitem = CreateItem(item);
mView.Items.Add(vitem);
mView.Tag = item;
OnDataBound(item);
}
}
protected virtual void OnItemClick(T e)
{
if (ItemClick != null)
ItemClick(this, e);
}
protected virtual void OnItemDoubleClick(T e)
{
if (ItemDoubleClick != null)
ItemDoubleClick(this, e);
}
protected virtual void OnDataBound(T e)
{
if (ItemDataBound != null)
ItemDataBound(this, e);
}
private void view_mousedown(object source, MouseEventArgs e)
{
mSelectItem = mView.GetItemAt(e.X, e.Y);
}
private void view_mouseclick(object source, MouseEventArgs e)
{
if (mSelectItem != null)
OnItemClick((T)mSelectItem.Tag);
}
private void view_mousedbclick(object source, MouseEventArgs e)
{
if (mSelectItem != null)
OnItemDoubleClick((T)mSelectItem.Tag);
}
private void view_selectchange(object source, EventArgs e)
{
SelectItems.Clear();
foreach (ListViewItem item in mView.SelectedItems)
{
SelectItems.Add((T)item.Tag);
}
}
public IList<T> SelectItems
{
get
{
return mSelectItems;
}
}
private IList<T> mSelectItems = new List<T>();
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace NClay.Windows
{
public delegate void EventDataListItemHandler<T>(object source,T e);
public abstract class DataListBase<T>
{
public event EventDataListItemHandler<T> ItemClick;
public event EventDataListItemHandler<T> ItemDoubleClick;
public event EventDataListItemHandler<T> ItemDataBound;
public DataListBase(ListView view)
{
mView = view;
Init();
}
public DataListBase(ListView view, EventDataListItemHandler<T> click,
EventDataListItemHandler<T> dbclick, EventDataListItemHandler<T> bound)
{
mView = view;
ItemClick += click;
ItemDoubleClick += dbclick;
ItemDataBound += bound;
Init();
}
private void Init()
{
mView.MouseDown += new MouseEventHandler(view_mousedown);
mView.MouseClick += new MouseEventHandler(view_mouseclick);
mView.MouseDoubleClick += new MouseEventHandler(view_mousedbclick);
mView.SelectedIndexChanged += new EventHandler(view_selectchange);
mView.Columns.Clear();
foreach (ColumnHeader item in Columns)
{
mView.Columns.Add(item);
}
}
private ListView mView;
private ListViewItem mSelectItem;
protected abstract ListView.ColumnHeaderCollection Columns
{
get;
}
protected abstract ListViewItem CreateItem(T e);
private IList<T> mDataSource;
public IList<T> DataSource
{
get
{
return mDataSource;
}
set
{
mDataSource = value;
OnDataBind();
}
}
private void OnDataBind()
{
mView.Items.Clear();
ListViewItem vitem;
foreach (T item in DataSource)
{
vitem = CreateItem(item);
mView.Items.Add(vitem);
mView.Tag = item;
OnDataBound(item);
}
}
protected virtual void OnItemClick(T e)
{
if (ItemClick != null)
ItemClick(this, e);
}
protected virtual void OnItemDoubleClick(T e)
{
if (ItemDoubleClick != null)
ItemDoubleClick(this, e);
}
protected virtual void OnDataBound(T e)
{
if (ItemDataBound != null)
ItemDataBound(this, e);
}
private void view_mousedown(object source, MouseEventArgs e)
{
mSelectItem = mView.GetItemAt(e.X, e.Y);
}
private void view_mouseclick(object source, MouseEventArgs e)
{
if (mSelectItem != null)
OnItemClick((T)mSelectItem.Tag);
}
private void view_mousedbclick(object source, MouseEventArgs e)
{
if (mSelectItem != null)
OnItemDoubleClick((T)mSelectItem.Tag);
}
private void view_selectchange(object source, EventArgs e)
{
SelectItems.Clear();
foreach (ListViewItem item in mView.SelectedItems)
{
SelectItems.Add((T)item.Tag);
}
}
public IList<T> SelectItems
{
get
{
return mSelectItems;
}
}
private IList<T> mSelectItems = new List<T>();
}
}