How to set gridgit tag propertiesgrid

I have a PropertyGrid that reflects the properties of my class.

I look at the PropertyValueChanged event and notice that the PropertyValueChangedEventArgs provides the GridItem that has changed.

That the GridItem has a tag property that I can get. I can't see how to set the Tag property of a GridItem to a value.

How do I set the Tag property for a GridItem?

+1


a source to share


1 answer


... My original answer was pretty far off, so I'm updating the whole thing in this update ...

Here's what I would do if I had to fulfill this requirement.

Create an attribute that will be used to pre-define the tag value of your GridItem; let's call it TagAttribute. It can be as simple as:

public class TagAttribute : Attribute
{
    public string TagValue { get; set; }

    public TagAttribute ( string tagValue )
    {
        TagValue = tagValue;
    }
}

      

To pre-define the value of a tag, you just need to decorate the desired property with that attribute.



public class MyAwesomeClass
{
    ...
    [TagAttribute( "This is my tag value." )]
    [CategoryAttribute( "Data" )]
    public string MyAwesomeProperty { get; set; }
    ...
}

      

I would then inherit the GridGrid property and override the OnPropertyValueChanged event to set the GridItem's Tag property to match the predefined TagAttribute.

public partial class InheritedPropertyGrid : PropertyGrid
{
    ...    
    protected override void OnPropertyValueChanged ( PropertyValueChangedEventArgs e )
    {
        var propertyInfo = SelectedObject.GetType().GetProperty( e.ChangedItem.PropertyDescriptor.Name );
        var tagAttribute = propertyInfo.GetCustomAttributes( typeof( TagAttribute ) , false );

        if ( tagAttribute != null )
            e.ChangedItem.Tag = ( (TagAttribute)tagAttribute[0] ).TagValue;

        base.OnPropertyValueChanged( e );
    }    
    ...
}

      

Now when you connect to the OnPropertyValueChanged of this "InheritedPropertyGrid", the Tag property will be set to whatever you specified in the property.

0


a source







All Articles