寻找一种方式来动态改变PropertyGrid中的字段名 [英] Looking for a way to dynamically change field names in PropertyGrid
问题描述
我有COM对象附加到属性网格。
I've got COM object attached to property grid.
Type typeObj = Type.GetTypeFromProgID(progIdService);
var obj = Activator.CreateInstance(typeObj);
propertyGrid1.SelectedObject = obj;
现在我需要一些方法来对象字段翻译成我的语言使用一些翻译。我试图用包装器对象,但与COM对象,我没有的PropertyInfo
,我只有 PropertyDescription
所以我还在原地寻找这样做的所有可能的变体。
Now I need some way to translate object fields into my language using some translator. I was trying to use wrapper around object but with COM object I have no PropertyInfo
, I have only PropertyDescription
so I'm still looking for all the possible variants of doing it.
推荐答案
你可以做的是重复使用的所以我的回答说明这个问题在这里的DynamicTypeDescriptor类:<一href="http://stackoverflow.com/questions/16422844/propertygrid-browsable-not-found-for-entity-framework-created-property-how-to-f">PropertyGrid可浏览找不到实体框架创建属性,怎么找呢?
What you could do is reuse the DynamicTypeDescriptor class described in my answer to this question here on SO: PropertyGrid Browsable not found for entity framework created property, how to find it?
是这样的:
DynamicTypeDescriptor dtp = new DynamicTypeDescriptor(typeObj);
// get current property definition and remove it
var current = dtp.Properties["ThePropertyToChange"];
dtp.RemoveProperty("ThePropertyToChange");
// add a new one, but change its display name
DynamicTypeDescriptor.DynamicProperty prop = new DynamicTypeDescriptor.DynamicProperty(dtp, current, obj);
prop.SetDisplayName("MyNewPropertyName");
dtp.AddProperty(prop);
propertyGrid1.SelectedObject = dtp.FromComponent(obj);
这篇关于寻找一种方式来动态改变PropertyGrid中的字段名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!