C# PropertyGrid - making all properties not bold
In a PropertyGrid form element, when I add properties to my categories, some appear in Bold.
Now, I know t开发者_JAVA技巧hat it suggests that they are the default values in that category. My question is how to make all properties not bold? I know one possible way is changing the DefaultValueAttribute attribute, but I was wondering if it can be done in a different way: this post suggests that I might have to use reflections, which is kind of mystical for me at the moment :) Thank you in advanceFor each property, you can add:
private bool ShouldSerialize{PropertyName}() { return false; }
Other than that, you are into the realm of custom PropertyDescriptor
implementations via ICustomTypeDescriptor
or TypeDescriptionProvider
.
Note that this pattern is used in a number of places, but in some (XmlSerializer
, for example), it is required to be a public
method.
Use [Default] Attribute on your own properties, otherwise you can do -at your very own risk- the following horrible hack.
Tried in .Net Framework 4.7.2. You'll loose the bold on categories aswell.
class MyNoBoldPropertyGrid : PropertyGrid
{
private void SetFontNoBold()
{
if (!DesignMode)
{
object lv = Controls[2];
Type lvType = lv.GetType();
FieldInfo pi = lvType.GetField("fontBold", BindingFlags.Instance | BindingFlags.NonPublic);
if (pi != null)
{
pi.SetValue(lv, Font);
}
}
}
protected override void OnPaint(PaintEventArgs pevent)
{
SetFontNoBold();
base.OnPaint(pevent);
}
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x210) // WM_PARENTNOTIFY
SetFontNoBold();
base.WndProc(ref m);
}
}
精彩评论