Completed
Last Updated: 31 Jul 2014 08:55 by ADMIN
ADMIN
Dess | Tech Support Engineer, Principal
Created on: 17 Jul 2014 11:14
Category: PropertyGrid
Type: Bug Report
0
FIX. RadPropertyGrid - ArgumentOutOfRangeException when setting the SelectedObject
To reproduce:
1. Add a RadPropertyGrid and a RadButton.
2. Create custom PropertyGridItemElement with custom PropertyGridValueElement in order to display permanently RadDropDownListElement
3. Use the following code snippet:

public Form1()
{
    InitializeComponent();

    this.radPropertyGrid1.CreateItemElement += radPropertyGrid1_CreateItemElement;
    this.radPropertyGrid1.Editing += radPropertyGrid1_Editing;
}

private void radPropertyGrid1_Editing(object sender,
    PropertyGridItemEditingEventArgs e)
{
    if (e.Item.Name == "Direction")
    {
        e.Cancel = true;
    }
}

private void radPropertyGrid1_CreateItemElement(object sender,
    CreatePropertyGridItemElementEventArgs e)
{
    PropertyGridItem item = (PropertyGridItem)e.Item;
    if (e.Item.Name == "Direction")
    {
        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
{
    RadDropDownListElement dropdown;

    protected override void CreateChildElements()
    {
        base.CreateChildElements();
 
        dropdown = new RadDropDownListElement();
        dropdown.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDownList;
        this.Children.Add(dropdown);

        dropdown.DataSource = Enum.GetValues(typeof(Direction));
        dropdown.SelectedIndexChanged += dropdown_SelectedIndexChanged;
    }

    private void dropdown_SelectedIndexChanged(object sender,
        Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
    {
        RadDropDownListElement ddle = sender as RadDropDownListElement;
        if (ddle != null && e.Position > -1)
        {
            PropertyGridItem item = this.VisualItem.Data as PropertyGridItem;
            if (item != null)
            {
                item.Value = ddle.Items[e.Position].DataBoundItem;
            }
        }
    }

    public override void Synchronize()
    {
        PropertyGridItem item = this.VisualItem.Data as PropertyGridItem;
        dropdown.SelectedValue = item.Value;
    }
}

public class Item
{
    public int Id { get; set; }

    public Direction Direction { get; set; }

    public string Title { get; set; }

    public Item(int id, string title, Direction direction)
    {
        this.Id = id;
        this.Title = title;
        this.Direction = direction;
    }
}

public enum Direction
{
    Up,
    Down,
    Left,
    Right
}

private void radButton1_Click(object sender, EventArgs e)
{
    this.radPropertyGrid1.SelectedObject = new Item(123, "Title", Direction.Left);
}

When you click the button once, the RadPropertyGrid displays the desired data. However, if you click the button once again, ArgumentOutOfRangeException occurs.

Workaround:wrap setting the SelectedObject in a SuspendLayout/ResumeLayout block:

this.radPropertyGrid1.SuspendLayout();
this.radPropertyGrid1.SelectedObject = new Item(123, "Title", Direction.Left);
this.radPropertyGrid1.ResumeLayout();
0 comments