PropertyGuid显示中文名

上传者: chlyzone | 上传时间: 2025-10-09 15:12:36 | 文件大小: 28KB | 文件类型: RAR
在.NET开发环境中,尤其是使用Visual Studio进行C#编程时,我们经常会遇到`PropertyGrid`控件。`PropertyGrid`是一个强大的工具,它用于显示和编辑对象的属性,通常用于设置程序内部的配置或者用户界面的自定义选项。在默认情况下,`PropertyGuid`是以其在代码中的唯一标识符(通常是GUID)显示的,这对于开发者来说是有意义的,但对于最终用户可能并不友好。本篇文章将详细介绍如何使`PropertyGuid`显示自定义的中文名称,以提升用户体验。 我们需要了解`PropertyGrid`的工作原理。`PropertyGrid`通过反射机制获取对象的所有公共属性,并以属性名和值的形式展示出来。如果一个属性有`DisplayName`、`Description`或`Category`等特性,`PropertyGrid`会使用这些特性来提供更友好的显示。 为了实现`PropertyGuid`显示中文名称,我们需要做以下几步: 1. **创建自定义特性**:创建一个继承自`System.ComponentModel.DescriptionAttribute`的新特性类,例如命名为`ChineseDescriptionAttribute`。在这个类中,我们可以添加一个字符串字段来存储中文描述。 ```csharp [AttributeUsage(AttributeTargets.Property)] public class ChineseDescriptionAttribute : DescriptionAttribute { public ChineseDescriptionAttribute(string chineseDescription) : base(chineseDescription) { ChineseDesc = chineseDescription; } private string ChineseDesc { get; set; } public override string Description { get => ChineseDesc; } } ``` 2. **应用自定义特性**:在需要显示中文名的属性上,添加这个自定义特性并传入对应的中文描述。 ```csharp public class MyClass { [ChineseDescription("我的中文名")] public Guid MyPropertyGuid { get; set; } } ``` 3. **处理`PropertyGrid`显示**:为了让`PropertyGrid`识别并使用我们的自定义特性,我们需要自定义一个`TypeConverter`。这个转换器会检查属性是否具有`ChineseDescriptionAttribute`,如果有,则使用其中的中文描述。 ```csharp public class ChineseDescriptionTypeConverter : TypeConverter { public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (destinationType == typeof(string) && context != null && context.Instance != null) { PropertyInfo propInfo = context.Instance.GetType().GetProperty(context.PropertyDescriptor.Name); if (propInfo != null) { var attr = Attribute.GetCustomAttribute(propInfo, typeof(ChineseDescriptionAttribute)) as ChineseDescriptionAttribute; if (attr != null) return attr.Description; } } return base.ConvertTo(context, culture, value, destinationType); } } ``` 4. **注册`TypeConverter`**:在类中使用`TypeConverterAttribute`为属性注册刚刚创建的转换器。 ```csharp public class MyClass { [TypeConverter(typeof(ChineseDescriptionTypeConverter))] [ChineseDescription("我的中文名")] public Guid MyPropertyGuid { get; set; } } ``` 5. **在UI中使用`PropertyGrid`**:在你的窗体中添加`PropertyGrid`控件,并将其`DataSource`属性设置为包含上述带有自定义特性的对象实例。 ```csharp private void InitializeComponent() { this.propertyGrid1 = new System.Windows.Forms.PropertyGrid(); // ... MyClass myInstance = new MyClass(); this.propertyGrid1.DataSource = myInstance; // ... } ``` 现在,当你运行程序,`PropertyGrid`会显示`MyPropertyGuid`的中文名称“我的中文名”而不是默认的GUID。 以上步骤展示了如何通过自定义特性及类型转换器实现`PropertyGrid`中`PropertyGuid`的中文显示。这种方法不仅适用于`Guid`类型,也可以应用于其他任何类型的属性,只需稍作修改即可。同时,这种方式也保留了原始属性的值,不会影响程序的正常运行和数据处理。

文件下载

资源详情

[{"title":"( 16 个子文件 28KB ) PropertyGuid显示中文名","children":[{"title":"bin","children":null,"spread":false},{"title":"Program.cs <span style='color:#111;'> 511B </span>","children":null,"spread":false},{"title":"obj","children":null,"spread":false},{"title":"Mssky.Controls.sln <span style='color:#111;'> 917B </span>","children":null,"spread":false},{"title":"TestForm.Designer.cs <span style='color:#111;'> 5.58KB </span>","children":null,"spread":false},{"title":"TestForm.cs <span style='color:#111;'> 868B </span>","children":null,"spread":false},{"title":"PropertyGrid","children":[{"title":"PropCnName.txt <span style='color:#111;'> 32.11KB </span>","children":null,"spread":false},{"title":"Descriptors.cs <span style='color:#111;'> 8.11KB </span>","children":null,"spread":false},{"title":"PropCnNameDict.cs <span style='color:#111;'> 1.73KB </span>","children":null,"spread":false},{"title":"PropertyGrid.cs <span style='color:#111;'> 5.57KB </span>","children":null,"spread":false}],"spread":true},{"title":"TestForm.resx <span style='color:#111;'> 5.68KB </span>","children":null,"spread":false},{"title":"Mssky.Controls.csproj <span style='color:#111;'> 4.04KB </span>","children":null,"spread":false},{"title":"Mssky.Controls.suo <span style='color:#111;'> 15.50KB </span>","children":null,"spread":false},{"title":"Properties","children":[{"title":"Resources.resx <span style='color:#111;'> 5.48KB </span>","children":null,"spread":false},{"title":"Settings.settings <span style='color:#111;'> 249B </span>","children":null,"spread":false},{"title":"AssemblyInfo.cs <span style='color:#111;'> 1.37KB </span>","children":null,"spread":false},{"title":"Settings.Designer.cs <span style='color:#111;'> 1.09KB </span>","children":null,"spread":false},{"title":"Resources.Designer.cs <span style='color:#111;'> 2.79KB </span>","children":null,"spread":false}],"spread":true}],"spread":true}]

评论信息

免责申明

【只为小站】的资源来自网友分享,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,【只为小站】 无法对用户传输的作品、信息、内容的权属或合法性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论 【只为小站】 经营者是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。
本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二条之规定,若资源存在侵权或相关问题请联系本站客服人员,zhiweidada#qq.com,请把#换成@,本站将给予最大的支持与配合,做到及时反馈和处理。关于更多版权及免责申明参见 版权及免责申明