1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > WPF使用枚举作多选设置的一种实现

WPF使用枚举作多选设置的一种实现

时间:2023-06-28 01:21:07

相关推荐

WPF使用枚举作多选设置的一种实现

基础类:

public class SelectItem{public string Key { set; get; }public string Value { set; get; }}public class SettingsEnumList:ObservableCollection<SelectItem>{public SettingsEnumList(){foreach (var item in typeof(SettingsEnum).GetEnumStructInfo()){Add(new SelectItem { Key = item.Id, Value = item.Description });}}}

public static class EnumExtensions{/// <summary>/// 根据枚举获取属性值列表/// </summary>/// <param name="enumType">枚举</param>/// <returns></returns>public static List<StatusDictionary> GetSelectList(Type enumType){var result = (from object e in Enum.GetValues(enumType)let e1 = (int)eselect new StatusDictionary{Id = e1.ToString(),Name = ((Enum)e).ToString(),Description = GetDescription((Enum)e),}).ToList();return result;}/// <summary>/// 根据枚举获取属性值列表/// </summary>/// <param name="enumType">枚举</param>/// <returns></returns>public static List<EnumStructInfo> GetEnumStructInfo(this Type enumType){var result = (from object e in Enum.GetValues(enumType)let e1 = (int)eselect new EnumStructInfo{Id = e1.ToString(),Name = ((Enum)e).ToString(),Description = GetDescription((Enum)e),AdditionalInfo = GetAdditionalInfo((Enum)e),}).ToList();return result;}/// <summary>/// 根据枚举成员获取属性描述/// </summary>/// <param name="e"></param>/// <returns></returns>public static string GetDescription(this Enum e){//获取枚举的Type类型对象var type = e.GetType();//获取枚举的所有字段var field = type.GetField(e.ToString());if (field == null) return e.ToString();//第二个参数true表示查找EnumDisplayNameAttribute的继承链var cus = field.GetCustomAttributes(typeof(DescriptionAttribute), true);//如果没有找到自定义属性,直接返回属性项的名称var des = cus.Length > 0 && cus[0] != null && cus[0] is DescriptionAttribute obj ? obj.Description : e.ToString();return des;}/// <summary>/// 获取公司定义的标准返回码/// </summary>/// <param name="e"></param>/// <returns></returns>public static string GetHashEventCode(this EventSpecificType e){return e.GetHashCode().ToString("0000");}public static string GetAdditionalInfo(this Enum e){//获取枚举的Type类型对象var type = e.GetType();//获取枚举的所有字段var field = type.GetField(e.ToString());//第二个参数true表示查找EnumDisplayNameAttribute的继承链var cus = field.GetCustomAttributes(typeof(AdditionalInfoAttribute), true);//如果没有找到自定义附加属性,直接返回属性项的名称var info = cus.Length > 0 && cus[0] is AdditionalInfoAttribute obj ? obj.AdditionalInfo : e.ToString();return info;}/// <summary>/// 获取业务定义的标准返回码/// </summary>/// <param name="e"></param>/// <returns></returns>public static int GetBusinessCode(this ApiResultEnum e){var busCode = int.TryParse(AppConfig.ProjectCode, out var prjCode) ? prjCode : 50;var code = (int)e;if (code == 0 || code == 1) return code;var result = busCode + code.ToString().PadLeft(ValuesConstant.BusinessCodeLength - busCode.ToString().Length, '0');return Convert.ToInt32(result);}}

XAML:

<common:SettingsEnumList x:Key="SettingsEnumList" /><ItemsControl Grid.Column="1"Width="550"HorizontalAlignment="Left"VerticalAlignment="Center"ItemsSource="{StaticResource SettingsEnumList}"><ItemsControl.ItemTemplate><DataTemplate><CheckBox Width="90"Margin="0,0,0,34"VerticalContentAlignment="Center"Command="{Binding Path=DataContext.SelectSettingCommand, ElementName=Main}"CommandParameter="{Binding Path=Key}"Content="{Binding Value}"FontSize="16"FontWeight="Light"><CheckBox.IsChecked><MultiBinding Converter="{StaticResource String2CheckBoxConverter}" Mode="OneWay"><MultiBinding.Bindings><Binding ElementName="Main" Path="DataContext.SelectedSettings" /><Binding Path="Key" /></MultiBinding.Bindings></MultiBinding></CheckBox.IsChecked></CheckBox></DataTemplate></ItemsControl.ItemTemplate><ItemsControl.ItemsPanel><ItemsPanelTemplate><WrapPanel /></ItemsPanelTemplate></ItemsControl.ItemsPanel></ItemsControl>

Converter:

public class String2CheckBoxConverter : IMultiValueConverter{public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture){bool result = false;if (values.Length >= 2){if (values[0] == null){values[0] = "";}result = values[0].ToString().Split(',').Contains(values[1].ToString());}return result;}public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture){throw new NotImplementedException();}}

VM:

private string _selectedSettings = string.Empty;public string SelectedSettings{get => _selectedSettings;set => SetPropertyNotify(ref _selectedSettings, value);}private async Task OnSelectSetting(string arg){await TaskEx.FromResult(0);var list = SelectedSettings?.Split(',').ToList();var isContain = list != null && list.Contains(arg);//每次点击处理选中项目var newSelected = "";if (isContain){list.Remove(arg);newSelected = string.Join(",", list.Where(o => !string.IsNullOrEmpty(o)).ToArray());SelectedSettings = newSelected;}else{SelectedSettings += "," + arg;}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。