A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
Well, I thought of building a list of a custom type that would be an intermediary type. So I rewrote my class to look like https://pastebin.com/x7cyaJmu . This doesn't do any better than what I had before. Still won't select. I tried changing entries member to be a HashTable with the key being the value. That caused the labels to be blank. The XAML code in the second sample is required for this to work.
namespace Org.WillPittenger.YouTubeDownloader.Ctrls.ForEnums
{
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
}
}
<ComboBox
x:Class="Org.WillPittenger.YouTubeDownloader.Ctrls.ForEnums.ComboBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Org.WillPittenger.YouTubeDownloader.Ctrls.ForEnums"
mc:Ignorable="d"
d:DesignHeight="450"
d:DesignWidth="800"
ItemTemplate="{DynamicResource OurItemTemplate}">
<ComboBox.Resources>
<DataTemplate x:Key="OurItemTemplate">
<Label
Content="{Binding DisplayText}"
ToolTip="{Binding TooltipText}" />
</DataTemplate>
</ComboBox.Resources>
</ComboBox>