using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using System.Windows.Controls; using System.Windows.Markup; namespace ComboBoxWPF { public class ViewModel : INotifyPropertyChanged { public ComboBoxItem CBStatus; private StatusEnum _SelectedEnumValue = StatusEnum.Wait; public StatusEnum SelectedEnumValue { get => this._SelectedEnumValue; set { this._SelectedEnumValue = value; OnPropertyChanged(nameof(Statuslnfo)); } } public string Statuslnfo { get => $"Selected: {SelectedEnumValue} - {(int)SelectedEnumValue}"; } public string StatuslnfoEasier { //get => $"Selected: {SelectedEnumValue} - {(int)SelectedEnumValue}"; get => CBStatus.Tag.ToString(); } public void ComboChanged() { OnPropertyChanged("StatuslnfoEasier"); } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged([CallerMemberName] string propName = "") => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName)); } public enum StatusEnum { Automatic = 20, Error = 21, Wait = 22, Blocked = 23, Remove = 24 } public class EnumBindingSourceExtension : MarkupExtension { private Type _enumType; public Type EnumType { get { return this._enumType; } set { if (value != this._enumType) if (value != null) { Type enumType = Nullable.GetUnderlyingType(value) ?? value; if (!enumType.IsEnum) throw new ArgumentException("Type must be for an Enum."); } this._enumType = value; } } public EnumBindingSourceExtension() { } public EnumBindingSourceExtension(Type enumType) => this.EnumType = enumType; public override object ProvideValue(IServiceProvider serviceProvider) { if (null == this._enumType) throw new InvalidOperationException("The EnumType must be specified."); Type actualEnumType = Nullable.GetUnderlyingType(this._enumType) ?? this._enumType; Array enumValues = Enum.GetValues(actualEnumType); if (actualEnumType == this._enumType) return enumValues; Array tempArray = Array.CreateInstance(actualEnumType, enumValues.Length + 1); enumValues.CopyTo(tempArray, 1); return tempArray; } } }