This is a migrated thread and some comments may be shown as answers.

How to use RadRadioButton In PropertyGrid ?

2 Answers 61 Views
PropertyGrid
This is a migrated thread and some comments may be shown as answers.
natcha
Top achievements
Rank 1
natcha asked on 30 Jun 2014, 03:32 AM
Hello ,

I used PropertyGrid. Is it possible to add RadRadioButton In PropertyGrid


Thanks,
Nat.

2 Answers, 1 is accepted

Sort by
0
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 02 Jul 2014, 01:22 PM
Hello Nat,

Thank you for writing.

You can achieve your requirement using custom PropertyGridItemElement with custom PropertyGridValueElement. Here is a sample implementation:
public Form1()
{
    InitializeComponent();
     
    this.radPropertyGrid1.PropertyGridElement.PropertyTableElement.ItemHeight = 37;
 
    this.radPropertyGrid1.CreateItemElement += radPropertyGrid1_CreateItemElement;
    this.radPropertyGrid1.Editing += radPropertyGrid1_Editing;
    this.radPropertyGrid1.SelectedObject = new Item(123, "Title", DeliveryType.PickUp);
}
 
private void radPropertyGrid1_Editing(object sender,
    PropertyGridItemEditingEventArgs e)
{
    if (e.Item.Name == "DeliveryType")
    {
        e.Cancel = true;
    }
}
 
private void radPropertyGrid1_CreateItemElement(object sender,
    CreatePropertyGridItemElementEventArgs e)
{
    if (e.Item.Name == "DeliveryType")
    {
        e.ItemElementType = typeof(CustomItemElement);
    }
}
 
public class CustomItemElement : PropertyGridItemElement
{
    protected override PropertyGridValueElement CreatePropertyGridValueElement()
    {
        return new CustomPropertyGridValueElement();
    }
 
    protected override Type ThemeEffectiveType
    {
        get
        {
            return typeof(PropertyGridItemElement);
        }
    }
}
 
public class CustomPropertyGridValueElement : PropertyGridValueElement
{
    StackLayoutElement stackPanel;
 
    protected override void CreateChildElements()
    {
        base.CreateChildElements();
 
        stackPanel = new StackLayoutElement();
        stackPanel.Orientation = Orientation.Vertical;
        foreach (var enumItem in Enum.GetValues(typeof(DeliveryType)))
        {
            RadRadioButtonElement rb = new RadRadioButtonElement();
            rb.Text = enumItem.ToString();
            rb.ToggleStateChanged += rb_ToggleStateChanged;
            stackPanel.Children.Add(rb);
        }
        this.Children.Add(stackPanel);
    }
 
    private void rb_ToggleStateChanged(object sender, StateChangedEventArgs args)
    {
        RadRadioButtonElement rb = sender as RadRadioButtonElement;
        PropertyGridItem item = this.VisualItem.Data as PropertyGridItem;
        if (item != null && rb.Text != item.FormattedValue && rb.ToggleState == ToggleState.On)
        {
            item.Value = rb.Text;
        }
    }
     
    public override void Synchronize()
    {
        PropertyGridItem item = this.VisualItem.Data as PropertyGridItem;
        foreach (RadRadioButtonElement rb in stackPanel.Children)
        {
            if (rb.Text == item.FormattedValue)
            {
                rb.ToggleState = ToggleState.On;
                       break;
            }
        }
    }
}
 
public class Item
{
    public int Id { get; set; }
 
    public string Title { get; set; }
 
    public DeliveryType DeliveryType { get; set; }
 
    public Item(int id, string title, DeliveryType deliveryType)
    {
        this.Id = id;
        this.Title = title;
        this.DeliveryType = deliveryType;
    }
}
 
public enum DeliveryType
{
    Delivery,
    PickUp,
}

I hope this information helps. Should you have further questions, I would be glad to help.

Regards,
Desislava
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
natcha
Top achievements
Rank 1
answered on 03 Jul 2014, 02:08 AM
Desislava, Thank you for your answer ^^
Tags
PropertyGrid
Asked by
natcha
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
natcha
Top achievements
Rank 1
Share this question
or