Looking for a way to dynamically change field names in PropertyGrid

為{幸葍}努か 提交于 2019-12-11 03:07:41

问题


I've got COM object attached to property grid.

Type typeObj = Type.GetTypeFromProgID(progIdService);
var obj = Activator.CreateInstance(typeObj);
propertyGrid1.SelectedObject = obj;

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.


回答1:


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?

like this:

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);



回答2:


I think you can use reflection to get the property names, although I haven't tried with COM objects yet. Be sure to include the System.Reflection namespace, then you can use it like that:

var props = myComObject.GetType().GetProperties();
foreach (var prop in props)
{
    MessageBox(prop.Name);
}


来源:https://stackoverflow.com/questions/18118630/looking-for-a-way-to-dynamically-change-field-names-in-propertygrid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!