| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- /*******************************************************************************
- * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
- * 类的信息:
- * 1.程序名称:ObjectSelectionWrapper.cs
- * 2.功能描述:类文件
- * 编辑履历:
- * 作者 日期 版本 修改内容
- * 陈晓野 2014/09/29 1.00 新建
- *******************************************************************************/
- using System;
- using System.ComponentModel;
- using System.Data;
- using System.Reflection;
- namespace Dongke.IBOSS.PRD.Basics.BaseControls
- {
- /// <summary>
- /// Used together with the ListSelectionWrapper in order to wrap data sources for a CheckBoxComboBox.
- /// It helps to ensure you don't add an extra "Selected" property to a class that don't really need or want that information.
- /// </summary>
- public class ObjectSelectionWrapper<TItem, TValue>
- {
- public ObjectSelectionWrapper(TItem item, ListSelectionWrapper<TItem, TValue> container)
- : base()
- {
- _Container = container;
- _Item = item;
- }
- #region PRIVATE PROPERTIES
- /// <summary>
- /// Used as a count indicator for the item. Not necessarily displayed.
- /// </summary>
- private int _Count = 0;
- /// <summary>
- /// Is this item selected.
- /// </summary>
- private bool _Selected = false;
- /// <summary>
- /// A reference to the wrapped item.
- /// </summary>
- private TItem _Item;
- /// <summary>
- /// The containing list for these selections.
- /// </summary>
- private ListSelectionWrapper<TItem, TValue> _Container;
- #endregion
- #region PUBLIC PROPERTIES
- /// <summary>
- /// An indicator of how many items with the specified status is available for the current filter level.
- /// Thaught this would make the app a bit more user-friendly and help not to miss items in Statusses
- /// that are not often used.
- /// </summary>
- public int Count
- {
- get { return _Count; }
- set { _Count = value; }
- }
- /// <summary>
- /// A reference to the item wrapped.
- /// </summary>
- public TItem Item
- {
- get { return _Item; }
- //set { _Item = value; }
- }
- public TValue Value
- {
- get
- {
- TValue Value = default(TValue);
- if (string.IsNullOrEmpty(_Container.ValueProperty))
- { //Value = Item.ToString();
- }
- else if (Item is DataRow) // A specific implementation for DataRow
- {
- var v = ((DataRow)((Object)Item))[_Container.ValueProperty];
- Value = (v is TValue) ? (TValue)v : Value;
- }
- else
- {
- bool isFind = false;
- PropertyDescriptorCollection PDs = TypeDescriptor.GetProperties(Item);
- foreach (PropertyDescriptor PD in PDs)
- if (PD.Name.CompareTo(_Container.ValueProperty) == 0)
- {
- var v = PD.GetValue(Item);
- Value = (v is TValue) ? (TValue)v : Value;
- isFind = true;
- break;
- }
- if (!isFind)
- {
- PropertyInfo PI = Item.GetType().GetProperty(_Container.ValueProperty);
- if (PI == null)
- throw new Exception(String.Format(
- "Property {0} cannot be found on {1}.",
- _Container.DisplayNameProperty,
- Item.GetType()));
- var v = PI.GetValue(Item, null);
- Value = (v is TValue) ? (TValue)v : Value;
- }
- }
- return Value;
- }
- }
- /// <summary>
- /// The item display value. If ShowCount is true, it displays the "Name [Count]".
- /// </summary>
- public string Name
- {
- get
- {
- string Name = null;
- if (string.IsNullOrEmpty(_Container.DisplayNameProperty))
- Name = Item.ToString();
- else if (Item is DataRow) // A specific implementation for DataRow
- Name = ((DataRow)((Object)Item))[_Container.DisplayNameProperty].ToString();
- else
- {
- bool isFind = false;
- PropertyDescriptorCollection PDs = TypeDescriptor.GetProperties(Item);
- foreach (PropertyDescriptor PD in PDs)
- if (PD.Name.CompareTo(_Container.DisplayNameProperty) == 0)
- {
- Name = (string)PD.GetValue(Item).ToString();
- isFind = true;
- break;
- }
- if (!isFind)
- {
- PropertyInfo PI = Item.GetType().GetProperty(_Container.DisplayNameProperty);
- if (PI == null)
- throw new Exception(String.Format(
- "Property {0} cannot be found on {1}.",
- _Container.DisplayNameProperty,
- Item.GetType()));
- Name = PI.GetValue(Item, null).ToString();
- }
- }
- return _Container.ShowCounts ? String.Format("{0} [{1}]", Name, Count) : Name;
- }
- }
- /// <summary>
- /// The textbox display value. The names concatenated.
- /// </summary>
- public string NameConcatenated
- {
- get { return _Container.SelectedNames; }
- }
- public TValue[] ValueConcatenated
- {
- get
- {
- return _Container.SelectedValues;
- }
- }
- /// <summary>
- /// Indicates whether the item is selected.
- /// </summary>
- public bool Selected
- {
- get { return _Selected; }
- set { _Selected = value; }
- }
- #endregion
- }
- }
|