Enum ItemTEnum 类
枚举项包装类:用于在 UI(如 ComboBox)中显示枚举的 Description 文本,
同时保留原始枚举值。
显示文本由 ToDescriptionTEnum(TEnum) 生成: 优先使用 [Description] 特性指定的字符串,未指定时返回枚举名称的全小写形式。
Definition
命名空间: Suresking.Utility
程序集: Suresking.Utility (在 Suresking.Utility.dll 中) 版本:1.3.22+a27276bb8ffde055e69ff608e98aafba279e1e99
程序集: Suresking.Utility (在 Suresking.Utility.dll 中) 版本:1.3.22+a27276bb8ffde055e69ff608e98aafba279e1e99
C#
public class EnumItem<TEnum>
where TEnum : struct, new()
- Inheritance
- Object EnumItemTEnum
类型参数
- TEnum
- 枚举类型
示例
C#
public enum StudentCategory
{
[Description("未分类")] Uncategorized = 0,
[Description("大一")] Freshman,
[Description("大二")] Sophomore,
}
// 生成用于 UI 绑定的列表
var items = EnumItem<StudentCategory>.FromEnum();
// 绑定到 ComboBox
// <ComboBox ItemsSource="{Binding Categories}"
// SelectedItem="{Binding SelectedCategory}" />构造函数
| EnumItemTEnum | 初始化 EnumItemTEnum 类的一个新实例 |
属性
方法
| FromEnum |
从枚举类型生成所有枚举项的列表,用于 UI 绑定。
列表顺序与 GetValuesTEnum 一致,即按枚举值大小升序排列。 |
| ToString |
返回 Display,使 ComboBox 等控件在未指定
DisplayMemberPath 时也能正确显示文本。
(重写 ObjectToString) |