/// <summary> /// Handles the <see cref="AddValue"/> event /// </summary> protected virtual void OnAddValue(AddValueEventArgs <T> e) { if (AddValue != null) { AddValue(this, e); } }
/// <summary> /// Creates the default data store for the list. /// </summary> /// <remarks>This is used to create a data store if one is not specified by the user. /// This can be used by subclasses to provide default items to populate the list.</remarks> /// <returns>The default data store.</returns> protected override IEnumerable <object> CreateDefaultDataStore() { var type = typeof(T); if (!type.IsEnum()) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "T must be an enumeration")); } var items = new ListItemCollection(); var values = Enum.GetValues(type); var names = Enum.GetNames(type); for (int i = 0; i < names.Length; i++) { var e = new AddValueEventArgs <T>((T)values.GetValue(i), true); OnAddValue(e); if (e.ShouldAdd) { items.Add(new EnumValue { Text = GetText != null ? GetText(e.Value) : names[i], Key = Convert.ToString(Convert.ToInt32(e.Value, CultureInfo.InvariantCulture), CultureInfo.InvariantCulture) }); } } if (SortAlphabetically) { items.Sort((x, y) => string.Compare(x.Text, y.Text, StringComparison.CurrentCultureIgnoreCase)); } return(items); }
protected override ListItemCollection CreateDefaultItems() { var type = typeof(T); if (!type.IsEnum) { throw new EtoException("T must be an enumeration"); } var items = new ListItemCollection(); var values = Enum.GetValues(type); var names = Enum.GetNames(type); for (int i = 0; i < names.Length; i++) { var e = new AddValueEventArgs <T> ((T)values.GetValue(i), true); if (e.ShouldAdd) { items.Add(new EnumValue { Text = names[i], Key = Convert.ToString(Convert.ToInt32(e.Value, CultureInfo.InvariantCulture), CultureInfo.InvariantCulture) }); } } items.Sort((x, y) => string.Compare(x.Text, y.Text, StringComparison.CurrentCultureIgnoreCase)); return(items); }
/// <summary> /// Creates the default items. /// </summary> /// <returns>The default items.</returns> protected override ListItemCollection CreateDefaultItems() { var type = typeof(T); if (!type.IsEnum()) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "T must be an enumeration")); } var items = new ListItemCollection(); var values = Enum.GetValues(type); var names = Enum.GetNames(type); var isFlags = type.GetCustomAttribute <FlagsAttribute>(true) != null; for (int i = 0; i < names.Length; i++) { var objValue = values.GetValue(i); if (isFlags && !IncludeNoneFlag && (int)objValue == 0) { continue; } var value = (T)objValue; var e = new AddValueEventArgs <T>(value, true); OnAddValue(e); if (e.ShouldAdd) { var enumValue = new EnumValue { Text = GetText != null?GetText(e.Value) : names[i], Key = Convert.ToString(Convert.ToInt32(e.Value, CultureInfo.InvariantCulture), CultureInfo.InvariantCulture), Value = value }; if (isFlags) { // show combined flags as tooltip var includedFlags = values.OfType <object>().Where(r => !Equals(r, value) && (((int)objValue & (int)r) != 0)).ToList(); if (includedFlags.Count > 1) { enumValue.ToolTip = string.Join(" | ", includedFlags.Select(r => GetText != null ? GetText((T)r) : Enum.GetName(type, r))); } } items.Add(enumValue); } } if (SortAlphabetically) { items.Sort((x, y) => string.Compare(x.Text, y.Text, StringComparison.CurrentCultureIgnoreCase)); } return(items); }