| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
-
- 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 属性
- /// <summary>
- /// 获取封装形式的数据源。
- /// </summary>
- public BindingSource BindingSource
- {
- get
- {
- return _bindingSource;
- }
- }
- #endregion 属性
- #region 构造函数
- /// <summary>
- /// 构造函数
- /// </summary>
- /// <param name="owner">Layout</param>
- public LayoutBoxPrintDataBinding(LayoutBox owner)
- {
- _owner = owner;
- _bindingSource = new BindingSource();
- }
- #endregion 构造函数
- #region 函数
- #region 公共函数
- /// <summary>
- /// 清理所有正在使用的资源
- /// </summary>
- public void Dispose()
- {
- UnWireEvents();
- _currencyManager = null;
- _owner = null;
- _props = null;
- _bindingSource = null;
- }
- /// <summary>
- /// 绑定打印数据源到文本Item
- /// </summary>
- /// <param name="textItem">文本Item</param>
- /// <returns>
- /// true : 绑定成功
- /// false : 绑定失败
- /// </returns>
- 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;
- }
- }
- /// <summary>
- /// 设置打印数据源的值到文本Item中
- /// </summary>
- /// <param name="textItem">文本Item</param>
- /// <returns>
- /// true : 设置成功
- /// false : 设置失败
- /// </returns>
- 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;
- }
- /// <summary>
- /// 绑定数据连接设置
- /// </summary>
- /// <param name="dataSource">数据源</param>
- /// <param name="dataMember">数据源列表的名称</param>
- 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();
- }
- /// <summary>
- /// 成员是否包含在数据源中
- /// </summary>
- /// <param name="newDataSource">数据源</param>
- /// <returns>
- /// true : 包含
- /// false : 不包含
- /// </returns>
- 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 私有函数
- /// <summary>
- /// 取得绑定数据源的索引
- /// </summary>
- /// <param name="dataFieldName">数据字段名</param>
- /// <returns>
- /// -1 : 数据字段名不存在
- /// 大于-1 : 数据字段对应的索引
- /// </returns>
- 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;
- }
- /// <summary>
- /// ListChanged事件
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- 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();
- }
- }
- /// <summary>
- /// PositionChanged事件
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- private void bindingSource_PositionChanged(object sender, EventArgs e)
- {
- _owner.RefreshItemsCurrentPositionPrintValue();
- }
- /// <summary>
- /// 从数据源检索数据
- /// </summary>
- /// <param name="textItem">文本Item</param>
- /// <param name="positionIndex">索引</param>
- /// <returns>数据源中对应的数据</returns>
- 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]);
- }
- /// <summary>
- /// 卸载事件
- /// </summary>
- private void UnWireEvents()
- {
- if (_bindingSource != null)
- {
- _bindingSource.PositionChanged -=
- new EventHandler(bindingSource_PositionChanged);
- _bindingSource.ListChanged -=
- new ListChangedEventHandler(bindingSource_ListChanged);
- }
- }
- /// <summary>
- /// 加载事件
- /// </summary>
- private void WireEvents()
- {
- if (_bindingSource != null)
- {
- _bindingSource.PositionChanged +=
- new EventHandler(bindingSource_PositionChanged);
- _bindingSource.ListChanged +=
- new ListChangedEventHandler(bindingSource_ListChanged);
- }
- }
- #endregion 私有函数
- #endregion 函数
- }
- }
|