【WPF】Enum与Converter的使用

在使用场景中大家都会遇到,下拉列表显示汉字,而存储使用的是对应的value值,从而转换就成了一个问题,接下来给大家带来一套的解决方案,方法可能不是特别高明,但是很实用,易读易维护,话不多说上代码!

Enum的使用

定义

定义的时候添加Description描述,这样可以一个Enum存在3个值,否则显示中文就需要将"Key"变为中文,但是做法有点落入下成了。

csharp 复制代码
 /// <summary>
 /// 动作类型字典
 /// </summary>
 public enum ActionType
 {
     [Description("待机,技能自身")]
     Standby = 0,

     [Description("行走,技能过程")]
     Walk = 1,

     [Description("跑步,技能目标")]
     Run = 2
}

赋值给下拉列表集合:

csharp 复制代码
ActionTypes = new ObservableCollection<ActionTypeModel>();

foreach (var enumData in Enum.GetValues(typeof(ActionType)))
{
    ActionTypes.Add(new ActionTypeModel() {Name = EnumHelper.GetDescriptionByEnum((ActionType)enumData), ActionCode = (int)(ActionType)enumData });
}

注意:博主定义了一个EnumHelper工具类,是用于获取Enum中Description的值的,没什么说的,网上找的

csharp 复制代码
using System;
using System.ComponentModel;

namespace EffectsPackTool.Common
{
    /// <summary>
    /// Enum帮助类
    /// </summary>
    public static class EnumHelper
    {
        /// <summary>
        /// 获取Enum的描述信息
        /// </summary>
        /// <param name="enumValue"></param>
        /// <returns></returns>
        public static string GetDescriptionByEnum(Enum enumValue)
        {
            string value = enumValue.ToString();
            System.Reflection.FieldInfo field = enumValue.GetType().GetField(value);
            object[] objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false);    //获取描述属性
            if (objs.Length == 0)    //当描述属性没有时,直接返回名称
                return value;
            DescriptionAttribute descriptionAttribute = (DescriptionAttribute)objs[0];
            return descriptionAttribute.Description;
        }
    }
}

Converter的使用

自定义Converter规则

没什么说的,简单,主要是继承IValueConverter

csharp 复制代码
using EffectsPackTool.Common;
using EffectsPackTool.Models.Enum;
using System;
using System.ComponentModel;
using System.Windows.Data;

namespace EffectsPackTool.Converter
{
    [TypeConverter(typeof(ActionTypeConverter))]
    public class ActionTypeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return "";

            return EnumHelper.GetDescriptionByEnum(((ActionType)value));
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }
    }
}

使用

先在资源列表里声明一个Converter模板,之后使用就可以了

XXView.xaml

csharp 复制代码
<UserControl.Resources>
    <converts:ActionTypeConverter x:Key="ActionCodeTypeConverter" />
</UserControl.Resources>

Binding="{Binding ActionCode, Converter={StaticResource ActionCodeTypeConverter}}"
相关推荐
码农水水8 小时前
得物Java面试被问:大规模数据的分布式排序和聚合
java·开发语言·spring boot·分布式·面试·php·wpf
时光慢煮10 小时前
行走在多端之间:基于 Flutter × OpenHarmony 的旅行记录应用实践 —— 旅行详情查看模块解析
flutter·华为·开源·wpf·openharmony
xiaobaishuoAI1 天前
分布式事务实战(Seata 版):解决分布式系统数据一致性问题(含代码教学)
大数据·人工智能·分布式·深度学习·wpf·geo
小北方城市网1 天前
微服务注册中心与配置中心实战(Nacos 版):实现服务治理与配置统一
人工智能·后端·安全·职场和发展·wpf·restful
cjp5601 天前
017.WPF使用自定义样式
wpf
故事不长丨1 天前
C#log4net详解:从入门到精通,配置、实战与框架对比
c#·.net·wpf·log4net·日志·winform·日志系统
cjp5601 天前
002.为C#动态链接库添加wpf窗体
microsoft·c#·wpf
bugcome_com1 天前
WPF控件模板
wpf
上海物联网2 天前
Prism WPF中的自定义区域适配器解决了什么问题?在项目中怎么实现一个自定义适配器
wpf
code bean2 天前
【C#高级】TCP请求-应答模式的WPF应用实战
tcp/ip·c#·wpf