Search code examples
c#propertiespropertygriddebuggerdisplay

Show DebuggerDisplay in PropertyGrid C#


I was wondering if it is possible to have the debugger display be the text for the class in the PropertyGrid?

I cant seem to find this answer anywhere.

Here is an example of what I have.

[DebuggerDisplay("FPS = {FPS}")]
[TypeConverter(typeof(ExpandableObjectConverter))]
public class DebugModule : Module
{
     public int FPS {get; set;}
}

This module is held in an Engine class so when I set the propertyGrid.SelectedObject = engineInstance, I would like to see in the property grid

Engine

+ DebugModuel | "FPS = 60"

FPS | 60

Solution

  • How about this, which displays the same text in the debugger and PropertyGrid:

    [DebuggerDisplay("{.}")]
    [TypeConverter(typeof(ExpandableObjectConverter))]
    public class DebugModule : Module
    {
        public int FPS { get; set; }
    
        public override string ToString() { return "FPS = " + FPS; }
    }
    

    Or if you need to use ToString for something else:

    [DebuggerDisplay("{DebugDisplayText}")]
    [TypeConverter(typeof(DebugModuleConverter))]
    public class DebugModule : Module
    {
        public int FPS { get; set; }
    
        private string DebugDisplayText { get { return "FPS = " + FPS; } }
    
        public class DebugModuleConverter : ExpandableObjectConverter {
            public override object ConvertTo(ITypeDescriptorContext context,
                    System.Globalization.CultureInfo culture, object value,
                    Type destinationType) {
                if(destinationType == typeof(string)) {
                    return ((DebugModule) value).DebugDisplayText;
                }
                return base.ConvertTo(context, culture, value, destinationType);
            }
        }
    }