
Two issues that I'm having that I can't seem to find a solution to.
1. If you have a an object A that contains object B and object B has say two public string properties. The PropertyGrid just shows the type of B rather than the contents of B. I have not seen any PropertyGrid property that states whether to allow for this or not.
2. I want to be able to set the DisplayName attribute at run time since this will depend on the language selected. So, what I did was create a derived class:
public class DisplayName2 : System.ComponentModel.DisplayNameAttribute
{
public DisplayName2()
{
}
public DisplayName2(string p_DisplayName)
{
DisplayNameValue = p_DisplayName;
}
public string RealDisplayName
{
get { return this.DisplayNameValue; }
set { this.DisplayNameValue = value; }
}
}
I can use it this way...
class Whatever {
[DisplayName2("InitialName")]
public string Name { get; set; }
}
then in code I would change this if necessary like this..
var v_Attr = typeof(Whatever).GetProperty("Name").GetCustomAttributes(false)[0] as DisplayName2;
v_Attr.RealDisplayName = "ChangedName";
or this way...
var v_Type = typeof(Whatever);
var v_FieldInfo = v_Type.GetProperty("Name");
var v_Attr = Attribute.GetCustomAttribute(v_FieldInfo, typeof(DisplayName2)) as DisplayName2;
v_Attr.RealDisplayName = "ChangedName";
either way, when the PropertyGrid is displayed, it shows, in the first column, "InitialName" instead of "ChangedName"
What is interesting is that I downloaded different PropertyGrid from a competitor to see what it did. It did the same thing.
How do I solve this problem?
1. If you have a an object A that contains object B and object B has say two public string properties. The PropertyGrid just shows the type of B rather than the contents of B. I have not seen any PropertyGrid property that states whether to allow for this or not.
2. I want to be able to set the DisplayName attribute at run time since this will depend on the language selected. So, what I did was create a derived class:
public class DisplayName2 : System.ComponentModel.DisplayNameAttribute
{
public DisplayName2()
{
}
public DisplayName2(string p_DisplayName)
{
DisplayNameValue = p_DisplayName;
}
public string RealDisplayName
{
get { return this.DisplayNameValue; }
set { this.DisplayNameValue = value; }
}
}
I can use it this way...
class Whatever {
[DisplayName2("InitialName")]
public string Name { get; set; }
}
then in code I would change this if necessary like this..
var v_Attr = typeof(Whatever).GetProperty("Name").GetCustomAttributes(false)[0] as DisplayName2;
v_Attr.RealDisplayName = "ChangedName";
or this way...
var v_Type = typeof(Whatever);
var v_FieldInfo = v_Type.GetProperty("Name");
var v_Attr = Attribute.GetCustomAttribute(v_FieldInfo, typeof(DisplayName2)) as DisplayName2;
v_Attr.RealDisplayName = "ChangedName";
either way, when the PropertyGrid is displayed, it shows, in the first column, "InitialName" instead of "ChangedName"
What is interesting is that I downloaded different PropertyGrid from a competitor to see what it did. It did the same thing.
How do I solve this problem?