Example #1
0
        public EnumComboBox()
        {
#if DEBUG
            // TEnum が確かに Enum 型であることのチェック
            if (!typeof(TEnum).GetTypeInfo().IsEnum)
            {
                throw new ArgumentException("TEnum must be an enumerated type");
            }
#endif

            // Enum 値とその名前をペアにした Dictionary を作る
            var items  = new Dictionary <TEnum, string>();
            var values = (TEnum[])Enum.GetValues(typeof(TEnum));
            foreach (var v in values)
            {
                items.Add(v, GetName(v));
            }

            // 上で作った Dictionary を、この ComboBox に表示する
            this.ItemsSource       = items;
            this.DisplayMemberPath = "Value"; // 表示するもの (名前)
            this.SelectedValuePath = "Key";   // 選択されたもの (Enum 値)

            // SelectedValue プロパティの変化を監視する
            m_watcher = new DependencyPropertyWatcher <object>(this, nameof(SelectedValue));
            m_watcher.PropertyChanged += OnSelectedValueChanged;
        }
        public static void OnPropertyChanged(object sender, DependencyPropertyChangedEventArgs args)
        {
            DependencyPropertyWatcher <T> source = (DependencyPropertyWatcher <T>)sender;

            source.PropertyChanged?.Invoke(source, EventArgs.Empty);
        }