Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public partial class ComboBox : System.Windows.Controls.ComboBox, System.ComponentModel.INotifyPropertyChanged
- {
- #region Constructors & Destructors
- public ComboBox()
- {
- ItemsSource = entries = new System.Collections.Generic.List<OneEntry>();
- InitializeComponent();
- }
- #endregion
- #region Events
- public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
- #endregion
- #region Constants
- private static class PropNames
- {
- public const string strEnumType = "EnumType";
- }
- #endregion
- #region Helper Types
- private class OneEntry
- {
- #region Constructors & Deconstructors
- public OneEntry(object enumVal, string strDisplayText, string strTooltipText)
- {
- System.Diagnostics.Debug.Assert(enumVal.GetType().IsEnum);
- this.enumVal = enumVal;
- this.strDisplayText = strDisplayText;
- this.strTooltipText = strTooltipText;
- }
- #endregion
- #region Members
- private readonly object enumVal;
- private readonly string strDisplayText;
- private readonly string strTooltipText;
- #endregion
- #region Properties
- public object EnumVal => enumVal;
- public string DisplayText => strDisplayText;
- public string TooltipText => strTooltipText;
- #endregion
- }
- #endregion
- #region Members
- #region Dependency Properties
- public static readonly System.Windows.DependencyProperty EnumTypeProperty = System.Windows.DependencyProperty.Register(PropNames.strEnumType,
- typeof(string), typeof(ComboBox), new System.Windows.PropertyMetadata(OnEnumTypePropChanged));
- #endregion
- private System.Type typeOfEnum = null;
- private System.Collections.Generic.List<OneEntry> entries;
- #endregion
- #region Properties
- [System.ComponentModel.Description("This is the enum type that the items are based on. Each one must have Org.WillPittenger.AdBlockPlusIniParserWPF.Core.Attr"
- + ".LocalizedDescAttribute applied to it.")]
- [System.ComponentModel.Category("Common")]
- public string EnumType
- {
- get => (string)GetValue(EnumTypeProperty);
- set => SetValue(EnumTypeProperty, value);
- }
- #endregion
- #region Methods
- private void GetEnumDesc(object objEnumValue, out string strDesc, out string strExtendedDesc)
- {
- System.Reflection.FieldInfo fieldInfo = objEnumValue.GetType().GetField(objEnumValue.ToString());
- object[] attribArray = fieldInfo.GetCustomAttributes(typeof(Attr.LocalizedDescAttribute), false);
- if(attribArray.Length == 0)
- {
- strDesc = objEnumValue.ToString();
- strExtendedDesc = "";
- }
- else
- {
- Attr.LocalizedDescAttribute attrib = attribArray[0] as Attr.LocalizedDescAttribute;
- strDesc = attrib.Desc;
- strExtendedDesc = attrib.ExtendedDesc;
- }
- }
- #endregion
- #region Event Handlers
- private void OnEnumTypePropChanged(string strNewVal)
- {
- System.Type typeProposed = System.Type.GetType(strNewVal);
- if(typeProposed == typeOfEnum)
- return;
- if(typeProposed == null || !typeProposed.IsEnum)
- return;
- typeOfEnum = typeProposed;
- entries.Capacity = typeOfEnum.GetEnumValues().Length;
- foreach(object objCurValInEnumType in typeOfEnum.GetEnumValues())
- {
- GetEnumDesc(objCurValInEnumType, out string strText, out string strTooltip);
- entries.Add(new OneEntry(objCurValInEnumType, strText, strTooltip));
- }
- UpdateLayout();
- }
- private static void OnEnumTypePropChanged(System.Windows.DependencyObject ctrlParent, System.Windows.DependencyPropertyChangedEventArgs e) =>
- ((ComboBox)ctrlParent).OnEnumTypePropChanged((string)e.NewValue);
- protected override void OnSelectionChanged(System.Windows.Controls.SelectionChangedEventArgs e) => base.OnSelectionChanged(e);
- #endregion
- }
Add Comment
Please, Sign In to add comment