How Do I Pass Data into a Custom PropertyGridItemElement

2 Answers 31 Views
PropertyGrid
Michael
Top achievements
Rank 1
Iron
Iron
Veteran
Michael asked on 01 Jul 2024, 02:06 PM

I have created a custom PropertyGridItemElement for my RadPropertyGrid to hold a button.  When the user clicks the button, the click handler performs some action, but the action is based on data that is known by the form that contains the propertygrid that contains the custom element.  How do I arrange for that data to be available to the button's Click handler?  The constructor of the custom element isn't even called explicitly -- it's implicit in 

PropertyGrid.CreateItemElement += (sender, args) =>
{
if (args.Item.Name == nameof(LabelWrapper.Button))
{
args.ItemElementType = typeof(CustomPropertyGridItemElement);
}
};

Thanks in advance.

2 Answers, 1 is accepted

Sort by
1
Accepted
Dinko | Tech Support Engineer
Telerik team
answered on 04 Jul 2024, 08:07 AM

Hello Michael,

To clarify your approach you have a custom PropertyGridValueElement in which you have added a button, similar to the approach demonstrated in the Custom Items help article. What comes to my mind is to use the Tag property of the custom PropertyGridValueElement. You can subscribe to the ItemFormatting event. In the event handler, you can check the e.VisualElement. If it is the custom one, you can get the custom PropertyGridValueElement and set the Tag property to an object or some other value depending on your requirement.

private void RadPropertyGrid1_ItemFormatting(object sender, PropertyGridItemFormattingEventArgs e)
{
    if (e.VisualElement is CustomPropertyGridItemElement)
    {
        ((e.VisualElement as CustomItemElement).ValueElement as CustomPropertyGridValueElement).Tag = "Test";
    }
}

Then in the click event of the button, you can check the Tag property. Something like that:

public class CustomPropertyGridValueElement : PropertyGridValueElement
{
    private RadButtonElement buttonElement;
    protected override void CreateChildElements()
    {
        base.CreateChildElements();

        buttonElement = new RadButtonElement();       
        buttonElement.Click += ClickEventHandler;
        this.Children.Add((buttonElement));
    }
	// other custom code
	
    protected override Type ThemeEffectiveType
    {
        get
        {
            return typeof(PropertyGridValueElement);
        }
    }
    private void ClickEventHandler(object sender, EventArgs e)
    {
        RadMessageBox.Show(this.Tag.ToString());           
    }
}

I hope that this approach will work for you.

Regards,
Dinko | Tech Support Engineer
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

0
Michael
Top achievements
Rank 1
Iron
Iron
Veteran
answered on 04 Jul 2024, 08:15 AM
That makes sense, I'll try it.  Thank you.
Tags
PropertyGrid
Asked by
Michael
Top achievements
Rank 1
Iron
Iron
Veteran
Answers by
Dinko | Tech Support Engineer
Telerik team
Michael
Top achievements
Rank 1
Iron
Iron
Veteran
Share this question
or