using System;
using System.ComponentModel;
using System.Globalization;
using System.Windows.Forms;
namespace Dongke.WinForm.Controls.InvoiceLayout
{
internal class LayoutBoxPrintDataBinding
{
#region 成员变量
private CurrencyManager _currencyManager; // 对象列表管理器
private LayoutBox _owner; // Layout
private PropertyDescriptorCollection _props; // 基本属性的描述列表集合
private BindingSource _bindingSource; // 封装形式的数据源
#endregion 成员变量
#region 属性
///
/// 获取封装形式的数据源。
///
public BindingSource BindingSource
{
get
{
return _bindingSource;
}
}
#endregion 属性
#region 构造函数
///
/// 构造函数
///
/// Layout
public LayoutBoxPrintDataBinding(LayoutBox owner)
{
_owner = owner;
_bindingSource = new BindingSource();
}
#endregion 构造函数
#region 函数
#region 公共函数
///
/// 清理所有正在使用的资源
///
public void Dispose()
{
UnWireEvents();
_currencyManager = null;
_owner = null;
_props = null;
_bindingSource = null;
}
///
/// 绑定打印数据源到文本Item
///
/// 文本Item
///
/// true : 绑定成功
/// false : 绑定失败
///
public bool BoundPrintFieldToTextItem(TextItem textItem)
{
if (textItem == null)
{
return false;
}
DataBoundField boundField = textItem.PrintBoundField;
boundField.IsDataBound = false;
boundField.BoundFieldIndex = -1;
if ((_bindingSource == null)
|| (_props == null) ||
(_bindingSource.DataSource == null))
{
textItem.PrintDataValue = null;
textItem.PrintBoundField = boundField;
return false;
}
// 获取绑定数据源的索引
int index = GetBoundFieldIndex(textItem.DataMember);
if (index < 0)
{
textItem.PrintDataValue = null;
textItem.PrintBoundField = boundField;
return false;
}
else
{
boundField.IsDataBound = true;
boundField.BoundFieldIndex = index;
boundField.BoundFieldConverter = _props[index].Converter;
boundField.ValueType = _props[index].PropertyType;
textItem.PrintBoundField = boundField;
// 获取数据源中的值
textItem.PrintDataValue = GetValue(textItem, _bindingSource.Position);
return true;
}
}
///
/// 设置打印数据源的值到文本Item中
///
/// 文本Item
///
/// true : 设置成功
/// false : 设置失败
///
public bool BoundPrintDataValueToTextItem(TextItem textItem)
{
if (textItem == null)
{
return false;
}
if ((!textItem.PrintBoundField.IsDataBound)
|| (textItem.PrintBoundField.BoundFieldIndex < 0)
|| (_props == null))
{
textItem.PrintDataValue = null;
return false;
}
textItem.PrintDataValue = GetValue(textItem, _bindingSource.Position);
return true;
}
///
/// 绑定数据连接设置
///
/// 数据源
/// 数据源列表的名称
public void SetDataConnection(object dataSource, string dataMember)
{
// 卸载事件
UnWireEvents();
if (dataMember == null)
{
dataMember = string.Empty;
}
_bindingSource.DataSource = dataSource;
_bindingSource.DataMember = dataMember;
_currencyManager = _bindingSource.CurrencyManager;
if (_currencyManager != null)
{
_props = _currencyManager.GetItemProperties();
}
else
{
_props = null;
}
// 加载事件
WireEvents();
}
///
/// 成员是否包含在数据源中
///
/// 数据源
///
/// true : 包含
/// false : 不包含
///
public bool ContainsDataMember(object newDataSource)
{
if (newDataSource != null)
{
if (_currencyManager == null || _props == null)
{
return false;
}
if (!string.IsNullOrEmpty(_bindingSource.DataMember)
&& _props[_bindingSource.DataMember] != null)
{
return false;
}
}
return true;
}
#endregion 公共函数
#region 私有函数
///
/// 取得绑定数据源的索引
///
/// 数据字段名
///
/// -1 : 数据字段名不存在
/// 大于-1 : 数据字段对应的索引
///
private int GetBoundFieldIndex(string dataFieldName)
{
if (_props == null || string.IsNullOrEmpty(dataFieldName))
{
return -1;
}
for (int i = 0; i < _props.Count; i++)
{
if (string.Compare(_props[i].Name, dataFieldName, true,
CultureInfo.InvariantCulture) == 0)
{
return i;
}
}
return -1;
}
///
/// ListChanged事件
///
/// 指定的对象
/// 提供的事件数据
private void bindingSource_ListChanged(object sender, ListChangedEventArgs e)
{
if (((e.ListChangedType == ListChangedType.PropertyDescriptorAdded)
|| (e.ListChangedType == ListChangedType.PropertyDescriptorDeleted))
|| (e.ListChangedType == ListChangedType.PropertyDescriptorChanged))
{
if (_currencyManager != null)
{
_props = _currencyManager.GetItemProperties();
_owner.RefreshItemsPrintBound();
}
}
else
{
_owner.RefreshItemsCurrentPositionPrintValue();
}
}
///
/// PositionChanged事件
///
/// 指定的对象
/// 提供的事件数据
private void bindingSource_PositionChanged(object sender, EventArgs e)
{
_owner.RefreshItemsCurrentPositionPrintValue();
}
///
/// 从数据源检索数据
///
/// 文本Item
/// 索引
/// 数据源中对应的数据
private object GetValue(TextItem textItem, int positionIndex)
{
if (_props == null
|| _bindingSource == null
|| _bindingSource.List == null
|| positionIndex < 0)
{
return null;
}
return _props[textItem.PrintBoundField.BoundFieldIndex].GetValue(
_bindingSource.List[positionIndex]);
}
///
/// 卸载事件
///
private void UnWireEvents()
{
if (_bindingSource != null)
{
_bindingSource.PositionChanged -=
new EventHandler(bindingSource_PositionChanged);
_bindingSource.ListChanged -=
new ListChangedEventHandler(bindingSource_ListChanged);
}
}
///
/// 加载事件
///
private void WireEvents()
{
if (_bindingSource != null)
{
_bindingSource.PositionChanged +=
new EventHandler(bindingSource_PositionChanged);
_bindingSource.ListChanged +=
new ListChangedEventHandler(bindingSource_ListChanged);
}
}
#endregion 私有函数
#endregion 函数
}
}