【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}}"
相关推荐
ALex_zry4 小时前
C++高性能日志与监控系统设计
c++·unity·wpf
zhojiew11 小时前
使用flink agent框架实现流式情感分析的示例
大数据·flink·wpf
海盗123411 小时前
ScottPlot在WPF的基本使用和中文乱码问题
c#·.net·wpf
俄城杜小帅11 小时前
C++线程异步和wpf中比较
java·c++·wpf
ZoeJoy813 小时前
WPF 从入门到实践:基础、ModernUI 与 MVVM 完全指南
c#·wpf
△曉風殘月〆2 天前
WPF Prism中的MVVM实现
wpf·mvvm
量子物理学2 天前
.NET8 中 WPF与ScottPlot 报表 的完美结合
.net·wpf
△曉風殘月〆2 天前
WPF Prism区域导航功能详解
wpf·mvvm
星河Cynthia2 天前
WPF基于resx资源文件的多语言实现
c#·wpf
量子物理学2 天前
WPF 标签预览可以显示图片运行后不显示
c#·wpf