【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}}"
相关推荐
dotent·2 小时前
一个 WPF 文档和工具窗口布局容器
wpf
c#上位机2 小时前
wpf之ComboBox
wpf
lindexi6 小时前
WPF 引用 ASP.NET Core 的 AOT 版本
wpf·asp.netcore
我好喜欢你~1 天前
WPF---数据模版
wpf
hqwest2 天前
C#WPF实战出真汁07--【系统设置】--菜品类型设置
开发语言·c#·wpf·grid设计·stackpanel布局
hqwest2 天前
C#WPF实战出真汁08--【消费开单】--餐桌面板展示
c#·wpf·ui设计·wpf界面设计
orangapple2 天前
WPF 打印报告图片大小的自适应(含完整示例与详解)
c#·wpf
三千道应用题3 天前
WPF&C#超市管理系统(6)订单详情、顾客注册、商品销售排行查询和库存提示、LiveChat报表
开发语言·c#·wpf
✎ ﹏梦醒͜ღ҉繁华落℘4 天前
开发WPF项目时遇到的问题总结
wpf
hqwest5 天前
C#WPF实战出真汁06--【系统设置】--餐桌类型设置
c#·.net·wpf·布局·分页·命令·viewmodel