ClassName in header WPG to DisplayNameAttribute

Nov 18, 2010 at 7:00 AM

PropertyGrid.cs (metod Refresh):

                    if (Template != null)
                    {
                        object myInstanceType = Template.FindName("PART_InstanceType", this);

                        if (myInstanceType != null && myInstanceType is TextBlock)
                        {
                            string myType = "";
                            Object[] myAttributes = Instance.GetType().GetCustomAttributes(false);
                            if (myAttributes.Length > 0)
                            {
                                foreach (Object attr in myAttributes)
                                {
                                    if (attr is System.ComponentModel.DisplayNameAttribute)
                                    {
                                        myType = ((System.ComponentModel.DisplayNameAttribute)attr).DisplayName;
                                    }
                                }
                            }
                            if (myType.Length == 0)
                            {
                                myType = Instance.GetType().ToString();
                                myType = myType.Substring(myType.LastIndexOf('.') + 1);
                            }
                            ((TextBlock) myInstanceType).Text = myType;
                        }

                        ...
 		} 
Sample:
    [System.ComponentModel.DisplayName("My super class")]
    public class MyClass
    {
        ...
    }
Nov 30, 2010 at 6:15 PM

Just what I was looking for, thank you very much!

Coordinator
Jan 5, 2011 at 9:20 AM
AlexShulg wrote:

PropertyGrid.cs (metod Refresh):

                    if (Template != null)
                    {
                        object myInstanceType = Template.FindName("PART_InstanceType", this);

                        if (myInstanceType != null && myInstanceType is TextBlock)
                        {
                            string myType = "";
                            Object[] myAttributes = Instance.GetType().GetCustomAttributes(false);
                            if (myAttributes.Length > 0)
                            {
                                foreach (Object attr in myAttributes)
                                {
                                    if (attr is System.ComponentModel.DisplayNameAttribute)
                                    {
                                        myType = ((System.ComponentModel.DisplayNameAttribute)attr).DisplayName;
                                    }
                                }
                            }
                            if (myType.Length == 0)
                            {
                                myType = Instance.GetType().ToString();
                                myType = myType.Substring(myType.LastIndexOf('.') + 1);
                            }
                            ((TextBlock) myInstanceType).Text = myType;
                        }

                        ...
 		} 
Sample:
    [System.ComponentModel.DisplayName("My super class")]
    public class MyClass
    {
        ...
    }

Patch added to newest version!