Hi,
Welcome to our Microsoft Q&A platform!
You can see my code,use Lable to bind SelectedValue:
Xaml:
<StackPanel>
<ComboBox Name="comboBox1" Width="150" ItemsSource="{Binding SelectedTypeDictionary}" DisplayMemberPath="Value" SelectedValuePath="Key" SelectedValue="{Binding ComboBoxSelectedValue}"/>
<Label Width="150" Content="{Binding ComboBoxSelectedValue}" Margin="30"></Label>
</StackPanel>
C# code:
public partial class MainWindow : Window,INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public object _comboBoxSelectedValue;
public object ComboBoxSelectedValue
{
get { return _comboBoxSelectedValue; }
set { _comboBoxSelectedValue = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("ComboBoxSelectedValue")); }
}
public Dictionary<Enum_Type, string> SelectedTypeDictionary { get; set; }
public MainWindow()
{
InitializeComponent();
SelectedTypeDictionary = new Dictionary<Enum_Type, string>();
SelectedTypeDictionary.Add(Enum_Type.SelectedOne, GetDescription(Enum_Type.SelectedOne));
SelectedTypeDictionary.Add(Enum_Type.SelectedTwo, GetDescription(Enum_Type.SelectedTwo));
SelectedTypeDictionary.Add(Enum_Type.SelectedThree,GetDescription(Enum_Type.SelectedThree));
this.DataContext = this;
}
public static T GetEnumAttribute<T>(Enum source) where T : Attribute
{
Type type = source.GetType();
var sourceName = Enum.GetName(type, source);
FieldInfo field = type.GetField(sourceName);
object[] attributes = field.GetCustomAttributes(typeof(T), false);
foreach (var o in attributes)
{
if (o is T)
return (T)o;
}
return null;
}
public static string GetDescription(Enum source)
{
var str = GetEnumAttribute<DescriptionAttribute>(source);
if (str == null)
return null;
return str.Description;
}
}
public enum Enum_Type
{
[Description("test1")]
SelectedOne,
[Description("test2")]
SelectedTwo,
[Description("test3")]
SelectedThree,
}
Thanks.