9.如何把枚举绑定到ComboBox控件上
1.添加ComboBox控件的枚举绑定扩展类
/// <summary> /// 绑定源的元素实体 /// </summary> /// <typeparam name="T"></typeparam> public class BindableSourceItem<T> { /// <summary> /// 描述 /// </summary> public string Description { get; set; } /// <summary> /// 值 /// </summary> public T Value { get; set; } public BindableSourceItem() { } public BindableSourceItem(string description, T value) { Description = description; Value = value; } }
/// <summary> /// ComboBox控件的枚举绑定扩展类 /// </summary> public static class ComboBoxExtension { /// <summary> /// 枚举绑定到ComboBox上 /// </summary> /// <typeparam name="T">枚举类型</typeparam> /// <param name="comboBox">comboBox</param> /// <param name="filter">条件</param> /// <param name="icon">图标</param> /// <exception cref="Exception"></exception> public static void Binding<T>(this ComboBox comboBox, Func<T, bool> filter = null, bool icon = false) where T : struct { var type = typeof(T); if (!type.IsEnum) { throw new Exception($"{type.FullName}必须为枚举类型"); } var items = type.GetBindableSourceItems<T>(filter); comboBox.DoBinding(items, icon); } public static IEnumerable<BindableSourceItem<T>> GetBindableSourceItems<T>(this object obj, Func<T, bool> filter = null) where T : struct { var type = typeof(T); if (!type.IsEnum) { throw new Exception($"{type.FullName}必须为枚举类型"); } var items = type.GetEnumValues().Cast<T>().Select(x => new BindableSourceItem<T>() { Description = x.GetEnumDescription(), Value = x, }); if (filter != null) { items = items.Where(p => filter(p.Value)); } return items; } public static string GetEnumDescription(this object obj) { Assert.NotNull(obj); var type = obj.GetType(); if (!type.IsEnum) { throw new Exception($"{type.FullName}必须为枚举类型"); } FieldInfo fieldInfo = obj.GetType().GetField(obj.ToString()); if (fieldInfo != null) { DescriptionAttribute attribute = Attribute.GetCustomAttribute(fieldInfo, typeof(DescriptionAttribute)) as DescriptionAttribute; if (attribute != null) { return attribute.Description; } else { return fieldInfo.Name; } } return string.Empty; } public static void DoBinding<T>(this ComboBox comboBox, IEnumerable<BindableSourceItem<T>> items, bool icon = false) { if (icon) { FrameworkElementFactory f = new FrameworkElementFactory(typeof(Image)); System.Windows.Data.Binding binding = new System.Windows.Data.Binding(); binding.Path = new PropertyPath(nameof(BindableSourceItem<T>.Description)); f.SetBinding(Image.SourceProperty, binding); f.SetValue(FrameworkElement.HeightProperty, 30.0); var template = new DataTemplate { VisualTree = f }; template.Seal(); comboBox.ItemTemplate = template; } else { comboBox.DisplayMemberPath = nameof(BindableSourceItem<T>.Description); } comboBox.SelectedValuePath = nameof(BindableSourceItem<T>.Value); comboBox.ItemsSource = items; } }
2.添加一个枚举
/// <summary> /// 图像缩放模式 /// </summary> public enum ZoomMode { [Description("自适应")] Uniform, [Description("100%")] Original, [Description("200%")] Percent200, [Description("300%")] Percent300, [Description("400%")] Percent400, [Description("500%")] Percent500, [Description("600%")] Percent600, [Description("700%")] Percent700, [Description("800%")] Percent800, }
3.使用
private void ComboBoxLodeAction(ComboBox obj) { obj.Binding<ZoomMode>(); }