| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- /*******************************************************************************
- * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
- * 类的信息:
- * 1.程序名称:ListSelectionWrapper.cs
- * 2.功能描述:类文件
- * 编辑履历:
- * 作者 日期 版本 修改内容
- * 陈晓野 2014/09/29 1.00 新建
- *******************************************************************************/
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Reflection;
- using System.ComponentModel;
- namespace Dongke.IBOSS.PRD.Basics.BaseControls
- {
- /// <summary>
- /// Maintains an additional "Selected" & "Count" value for each item in a List.
- /// Useful in the CheckBoxComboBox. It holds a reference to the List[Index] Item and
- /// whether it is selected or not.
- /// It also caters for a Count, if needed.
- /// </summary>
- /// <typeparam name="TSelectionWrapper"></typeparam>
- public class ListSelectionWrapper<TItem, TValue> : List<ObjectSelectionWrapper<TItem, TValue>>
- {
- #region CONSTRUCTOR
- ///// <summary>
- ///// No property on the object is specified for display purposes, so simple ToString() operation
- ///// will be performed. And no Counts will be displayed
- ///// </summary>
- //public ListSelectionWrapper(IEnumerable source) : this(source, false) { }
- ///// <summary>
- ///// No property on the object is specified for display purposes, so simple ToString() operation
- ///// will be performed.
- ///// </summary>
- //private ListSelectionWrapper(IEnumerable source, bool showCounts)
- // : base()
- //{
- // _Source = source;
- // _ShowCounts = showCounts;
- // if (_Source is IBindingList)
- // ((IBindingList)_Source).ListChanged += new ListChangedEventHandler(ListSelectionWrapper_ListChanged);
- // Populate();
- //}
- ///// <summary>
- ///// A Display "Name" property is specified. ToString() will not be performed on items.
- ///// This is specifically useful on DataTable implementations, or where PropertyDescriptors are used to read the values.
- ///// If a PropertyDescriptor is not found, a Property will be used.
- ///// </summary>
- //public ListSelectionWrapper(IEnumerable source, string usePropertyAsDisplayName) : this(source, false, usePropertyAsDisplayName) { }
- ///// <summary>
- ///// A Display "Name" property is specified. ToString() will not be performed on items.
- ///// This is specifically useful on DataTable implementations, or where PropertyDescriptors are used to read the values.
- ///// If a PropertyDescriptor is not found, a Property will be used.
- ///// </summary>
- //public ListSelectionWrapper(IEnumerable source, bool showCounts, string usePropertyAsDisplayName)
- // : this(source, showCounts)
- //{
- // _DisplayNameProperty = usePropertyAsDisplayName;
- //}
- public ListSelectionWrapper(IEnumerable source, string usePropertyAsDisplayName, string valueProperty)
- : base()
- {
- _Source = source;
- _ShowCounts = false;
- _DisplayNameProperty = usePropertyAsDisplayName;
- _ValueProperty = valueProperty;
- if (_Source is IBindingList)
- ((IBindingList)_Source).ListChanged += new ListChangedEventHandler(ListSelectionWrapper_ListChanged);
- Populate();
- }
- #endregion
- #region PRIVATE PROPERTIES
- /// <summary>
- /// Is a Count indicator used.
- /// </summary>
- private bool _ShowCounts;
- /// <summary>
- /// The original List of values wrapped. A "Selected" and possibly "Count" functionality is added.
- /// </summary>
- private IEnumerable _Source;
- /// <summary>
- /// Used to indicate NOT to use ToString(), but read this property instead as a display value.
- /// </summary>
- private string _DisplayNameProperty = null;
- private string _ValueProperty = null;
- #endregion
- #region PUBLIC PROPERTIES
- /// <summary>
- /// When specified, indicates that ToString() should not be performed on the items.
- /// This property will be read instead.
- /// This is specifically useful on DataTable implementations, where PropertyDescriptors are used to read the values.
- /// </summary>
- public string DisplayNameProperty
- {
- get { return _DisplayNameProperty; }
- set { _DisplayNameProperty = value; }
- }
- public string ValueProperty
- {
- get
- {
- return _ValueProperty;
- }
- set
- {
- _ValueProperty = value;
- }
- }
- /// <summary>
- /// Builds a concatenation list of selected items in the list.
- /// </summary>
- public string SelectedNames
- {
- get
- {
- string Text = "";
- foreach (ObjectSelectionWrapper<TItem, TValue> Item in this)
- if (Item.Selected)
- Text += (
- string.IsNullOrEmpty(Text)
- ? String.Format("\"{0}\"", Item.Name)
- : String.Format(" & \"{0}\"", Item.Name));
- return Text;
- }
- }
- public TValue[] SelectedValues
- {
- get
- {
- List<TValue> Values = new List<TValue>();
- foreach (ObjectSelectionWrapper<TItem, TValue> Item in this)
- if (Item.Selected)
- Values.Add(Item.Value);
- return Values.ToArray();
- }
- }
- /// <summary>
- /// Indicates whether the Item display value (Name) should include a count.
- /// </summary>
- public bool ShowCounts
- {
- get { return _ShowCounts; }
- set { _ShowCounts = value; }
- }
- #endregion
- #region HELPER MEMBERS
- /// <summary>
- /// Reset all counts to zero.
- /// </summary>
- public void ClearCounts()
- {
- foreach (ObjectSelectionWrapper<TItem, TValue> Item in this)
- Item.Count = 0;
- }
- /// <summary>
- /// Creates a ObjectSelectionWrapper item.
- /// Note that the constructor signature of sub classes classes are important.
- /// </summary>
- /// <param name="Object"></param>
- /// <returns></returns>
- private ObjectSelectionWrapper<TItem, TValue> CreateSelectionWrapper(IEnumerator Object)
- {
- Type[] Types = new Type[] { typeof(TItem), this.GetType() };
- ConstructorInfo CI = typeof(ObjectSelectionWrapper<TItem, TValue>).GetConstructor(Types);
- if (CI == null)
- throw new Exception(String.Format(
- "The selection wrapper class {0} must have a constructor with ({1} Item, {2} Container) parameters.",
- typeof(ObjectSelectionWrapper<TItem, TValue>),
- typeof(TItem),
- this.GetType()));
- object[] parameters = new object[] { Object.Current, this };
- object result = CI.Invoke(parameters);
- return (ObjectSelectionWrapper<TItem, TValue>)result;
- }
- public ObjectSelectionWrapper<TItem, TValue> FindObjectWithItem(TItem Object)
- {
- return Find(new Predicate<ObjectSelectionWrapper<TItem, TValue>>(
- delegate(ObjectSelectionWrapper<TItem, TValue> target)
- {
- return target.Item.Equals(Object);
- }));
- }
- public ObjectSelectionWrapper<TItem, TValue> FindObjectWithValue(TValue Object)
- {
- return Find(new Predicate<ObjectSelectionWrapper<TItem, TValue>>(
- delegate(ObjectSelectionWrapper<TItem, TValue> target)
- {
- return target.Value.Equals(Object);
- }));
- }
- /*
- public TSelectionWrapper FindObjectWithKey(object key)
- {
- return FindObjectWithKey(new object[] { key });
- }
- public TSelectionWrapper FindObjectWithKey(object[] keys)
- {
- return Find(new Predicate<TSelectionWrapper>(
- delegate(TSelectionWrapper target)
- {
- return
- ReflectionHelper.CompareKeyValues(
- ReflectionHelper.GetKeyValuesFromObject(target.Item, target.Item.TableInfo),
- keys);
- }));
- }
- public object[] GetArrayOfSelectedKeys()
- {
- List<object> List = new List<object>();
- foreach (TSelectionWrapper Item in this)
- if (Item.Selected)
- {
- if (Item.Item.TableInfo.KeyProperties.Length == 1)
- List.Add(ReflectionHelper.GetKeyValueFromObject(Item.Item, Item.Item.TableInfo));
- else
- List.Add(ReflectionHelper.GetKeyValuesFromObject(Item.Item, Item.Item.TableInfo));
- }
- return List.ToArray();
- }
- public T[] GetArrayOfSelectedKeys<T>()
- {
- List<T> List = new List<T>();
- foreach (TSelectionWrapper Item in this)
- if (Item.Selected)
- {
- if (Item.Item.TableInfo.KeyProperties.Length == 1)
- List.Add((T)ReflectionHelper.GetKeyValueFromObject(Item.Item, Item.Item.TableInfo));
- else
- throw new LibraryException("This generator only supports single value keys.");
- // List.Add((T)ReflectionHelper.GetKeyValuesFromObject(Item.Item, Item.Item.TableInfo));
- }
- return List.ToArray();
- }
- */
- private void Populate()
- {
- Clear();
- /*
- for(int Index = 0; Index <= _Source.Count -1; Index++)
- Add(CreateSelectionWrapper(_Source[Index]));
- */
- IEnumerator Enumerator = _Source.GetEnumerator();
- if (Enumerator != null)
- while (Enumerator.MoveNext())
- Add(CreateSelectionWrapper(Enumerator));
- }
- #endregion
- #region EVENT HANDLERS
- private void ListSelectionWrapper_ListChanged(object sender, ListChangedEventArgs e)
- {
- switch (e.ListChangedType)
- {
- case ListChangedType.ItemAdded:
- Add(CreateSelectionWrapper((IEnumerator)((IBindingList)_Source)[e.NewIndex]));
- break;
- case ListChangedType.ItemDeleted:
- Remove(FindObjectWithItem((TItem)((IBindingList)_Source)[e.OldIndex]));
- break;
- case ListChangedType.Reset:
- Populate();
- break;
- }
- }
- #endregion
- }
- }
|