开发者

C# PropertyGrid - making all properties not bold

开发者 https://www.devze.com 2023-04-05 20:17 出处:网络
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

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 advance


For 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);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消