Guest User

Will Pittenger

a guest
Mar 21st, 2020
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.69 KB | None | 0 0
  1.     public partial class ComboBox : System.Windows.Controls.ComboBox, System.ComponentModel.INotifyPropertyChanged
  2.     {
  3.         #region Constructors & Destructors
  4.             public ComboBox()
  5.             {
  6.                 ItemsSource = entries = new System.Collections.Generic.List<OneEntry>();
  7.  
  8.                 InitializeComponent();
  9.             }
  10.         #endregion
  11.  
  12.         #region Events
  13.             public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
  14.         #endregion
  15.  
  16.         #region Constants
  17.             private static class PropNames
  18.             {
  19.                 public const string strEnumType = "EnumType";
  20.             }
  21.         #endregion
  22.  
  23.         #region Helper Types
  24.             private class OneEntry
  25.             {
  26.                 #region Constructors & Deconstructors
  27.                     public OneEntry(object enumVal, string strDisplayText, string strTooltipText)
  28.                     {
  29.                         System.Diagnostics.Debug.Assert(enumVal.GetType().IsEnum);
  30.  
  31.                         this.enumVal = enumVal;
  32.                         this.strDisplayText = strDisplayText;
  33.                         this.strTooltipText = strTooltipText;
  34.                     }
  35.                 #endregion
  36.  
  37.                 #region Members
  38.                     private readonly object enumVal;
  39.                     private readonly string strDisplayText;
  40.                     private readonly string strTooltipText;
  41.                 #endregion
  42.  
  43.                 #region Properties
  44.                     public object EnumVal => enumVal;
  45.                     public string DisplayText => strDisplayText;
  46.                     public string TooltipText => strTooltipText;
  47.                 #endregion
  48.             }
  49.         #endregion
  50.  
  51.         #region Members
  52.             #region Dependency Properties
  53.                 public static readonly System.Windows.DependencyProperty EnumTypeProperty = System.Windows.DependencyProperty.Register(PropNames.strEnumType,
  54.                     typeof(string), typeof(ComboBox), new System.Windows.PropertyMetadata(OnEnumTypePropChanged));
  55.             #endregion
  56.  
  57.             private System.Type typeOfEnum = null;
  58.  
  59.             private System.Collections.Generic.List<OneEntry> entries;
  60.         #endregion
  61.  
  62.         #region Properties
  63.             [System.ComponentModel.Description("This is the enum type that the items are based on.  Each one must have Org.WillPittenger.AdBlockPlusIniParserWPF.Core.Attr"
  64.                 + ".LocalizedDescAttribute applied to it.")]
  65.             [System.ComponentModel.Category("Common")]
  66.             public string EnumType
  67.             {
  68.                 get => (string)GetValue(EnumTypeProperty);
  69.  
  70.                 set => SetValue(EnumTypeProperty, value);
  71.             }
  72.         #endregion
  73.  
  74.         #region Methods
  75.             private void GetEnumDesc(object objEnumValue, out string strDesc, out string strExtendedDesc)
  76.             {
  77.                 System.Reflection.FieldInfo fieldInfo = objEnumValue.GetType().GetField(objEnumValue.ToString());
  78.  
  79.                 object[] attribArray = fieldInfo.GetCustomAttributes(typeof(Attr.LocalizedDescAttribute), false);
  80.  
  81.                 if(attribArray.Length == 0)
  82.                 {
  83.                     strDesc = objEnumValue.ToString();
  84.                     strExtendedDesc = "";
  85.                 }
  86.                 else
  87.                 {
  88.                     Attr.LocalizedDescAttribute attrib = attribArray[0] as Attr.LocalizedDescAttribute;
  89.  
  90.                     strDesc = attrib.Desc;
  91.                     strExtendedDesc = attrib.ExtendedDesc;
  92.                 }
  93.             }
  94.         #endregion
  95.  
  96.         #region Event Handlers
  97.             private void OnEnumTypePropChanged(string strNewVal)
  98.             {
  99.                 System.Type typeProposed = System.Type.GetType(strNewVal);
  100.  
  101.                 if(typeProposed == typeOfEnum)
  102.                     return;
  103.  
  104.                 if(typeProposed == null || !typeProposed.IsEnum)
  105.                     return;
  106.  
  107.                 typeOfEnum = typeProposed;
  108.  
  109.                 entries.Capacity = typeOfEnum.GetEnumValues().Length;
  110.  
  111.                 foreach(object objCurValInEnumType in typeOfEnum.GetEnumValues())
  112.                 {
  113.                     GetEnumDesc(objCurValInEnumType, out string strText, out string strTooltip);
  114.  
  115.                     entries.Add(new OneEntry(objCurValInEnumType, strText, strTooltip));
  116.                 }
  117.  
  118.                 UpdateLayout();
  119.             }
  120.  
  121.             private static void OnEnumTypePropChanged(System.Windows.DependencyObject ctrlParent, System.Windows.DependencyPropertyChangedEventArgs e) =>
  122.                 ((ComboBox)ctrlParent).OnEnumTypePropChanged((string)e.NewValue);
  123.  
  124.             protected override void OnSelectionChanged(System.Windows.Controls.SelectionChangedEventArgs e) => base.OnSelectionChanged(e);
  125.         #endregion
  126.     }
Add Comment
Please, Sign In to add comment