【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}}"
相关推荐
samble1 天前
从零搭建半导体设备上位机(十二):参数设置页,表单校验、范围校验与配方联动
wpf·数据校验·validationrule
samble9 天前
从零搭建半导体设备上位机(九):拖拽与交互,Adorner、DragDrop 与晶圆拖放信息
wpf·拖拽·adorner
郝学胜-神的一滴11 天前
AI 编程智能体 27:吃透MCP通信机制
人工智能·python·langchain·wpf·numpy·scikit-learn
波力海苔夹心脆67512 天前
C# WPF 资源字典详解:ResourceDictionary、MergedDictionaries 与自定义控件案例
开发语言·c#·wpf·入门教程·resource·资源字典
陈仓主教12 天前
# WPF CheckBox 控件详解与使用—WPF 中的控件(6)--复选框控件(CheckBox)
wpf
光泽雨14 天前
WPF 音视频四大核心方案对比
wpf·音视频
浪扼飞舟14 天前
WPF(数据绑定)
wpf
samble15 天前
从零搭建半导体设备上位机(四):单实例、启动流程与配置文件,App 里那 526 行到底在干什么
wpf·配置文件·单实例
浪扼飞舟16 天前
WPF(控件的继承树、内容控件、布局控件)
wpf
avi911119 天前
电影开源Jevdo - 源码介绍(vue版)没有android 和 没有wpf
android·wpf·框架·数据爬虫·meego·电影管理