簡體   English   中英

如何設置propertygrid griditem標簽

[英]How to set propertygrid griditem tag

我有一個PropertyGrid可以反映我班級的屬性。

我正在觀看PropertyValueChanged事件,並注意到PropertyValueChangedEventArgs提供了已更改的GridItem。

該GridItem具有我可以獲取的Tag屬性。 我看不到如何將GridItem的Tag屬性設置為一個值。

如何設置GridItem的Tag屬性?

...我的原始答案還差得很遠,因此我將在此更新中對整個過程進行改進...

如果需要滿足此要求,這就是我會做的事情。

創建一個將用於預定義GridItem的Tag值的屬性; 我們稱之為TagAttribute 它可以像這樣簡單:

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

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

要預定義Tag值,您只需要使用此屬性裝飾所需的屬性。

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

然后,我將繼承PropertyGrid並重寫OnPropertyValueChanged事件,以將GridItemTag屬性設置為與預定義的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 );
    }    
    ...
}

現在,當您掛接到此“ InheritedPropertyGrid”的OnPropertyValueChanged時, Tag屬性將設置為您在該屬性上定義的屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM