How to use RadRadioButton In PropertyGrid ?

3 posts, 1 answers
  1. natcha
    natcha avatar
    2 posts
    Member since:
    Jun 2014

    Posted 29 Jun 2014 Link to this post

    Hello ,

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


    Thanks,
    Nat.
  2. Answer
    Dess
    Admin
    Dess avatar
    1609 posts

    Posted 02 Jul 2014 Link to this post

    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.
     
  3. UI for WinForms is Visual Studio 2017 Ready
  4. natcha
    natcha avatar
    2 posts
    Member since:
    Jun 2014

    Posted 02 Jul 2014 in reply to Dess Link to this post

    Desislava, Thank you for your answer ^^
Back to Top