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

Dynamic TypeConverter.

6 Answers 337 Views
PropertyGrid
This is a migrated thread and some comments may be shown as answers.
Hugues
Top achievements
Rank 1
Hugues asked on 10 Dec 2014, 09:41 PM
Is is possible to create an Expandable drop down converter object at Runtime.

6 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 15 Dec 2014, 02:29 PM
Hello Hugues,

Thank you for writing.

If I understand your requirement correctly, you are trying to create an ExpandableObjectConverter and assign it to a specific property at run time. For this purpose, you can use the following code snippet:
public Form1()
{
    InitializeComponent();
 
    PropertyDescriptor prop = TypeDescriptor.GetProperties(typeof(Item))["Cell"];
    TypeConverter tc = prop.Converter;
    typeof(PropertyDescriptor).GetField("converter", BindingFlags.NonPublic
        | BindingFlags.Instance).SetValue(prop, new MyConverter());
 
    this.radPropertyGrid1.SelectedObject = new Item(123,new Cell(3.7m,4.2m),"test");
}
 
public class Item
{
    public int Id { get; set; }
     
    public Cell Cell { get; set; }
 
    public string Title { get; set; }
   
    public Item(int id, Cell cell, string title)
    {
        this.Id = id;
        this.Cell = cell;
        this.Title = title;
    }
}
 
public class Cell
{
    public decimal X { get; set; }
 
    public decimal Y { get; set; }
 
    public Cell(decimal x, decimal y)
    {
        this.X = x;
        this.Y = y;
    }
}
 
public class MyConverter:ExpandableObjectConverter
{
    public override bool CanConvertTo(ITypeDescriptorContext context,
        System.Type destinationType)
    {
        if (destinationType == typeof(string))
            return true;
 
        return base.CanConvertTo(context, destinationType);
    }
 
    public override object ConvertTo(ITypeDescriptorContext context,
        CultureInfo culture,
        object value,
        System.Type destinationType)
    {
        Cell cell = value as Cell;
        if (destinationType == typeof(System.String) &&
            value != null)
        {
            return ("X: " + cell.X + " Y: " + cell.Y);
        }
        return base.ConvertTo(context, culture, value, destinationType);
    }
 
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        if (sourceType == typeof(string))
        {
            return true;
        }
        return base.CanConvertFrom(context, sourceType);
    }
 
    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
    {
        string[] tokens = (value + "").Split(new string[] { "X: ", "Y: " }, StringSplitOptions.RemoveEmptyEntries) ;
        if (tokens.Length == 2)
        {
            decimal x;
            decimal y;
            if (decimal.TryParse(tokens[0], out x) &&
                decimal.TryParse(tokens[1], out y))
            {
                return new Cell(x,y);
            }
        }
 
        return base.ConvertFrom(context, culture, value);
    }
}

If it is not the exact case, I would kindly ask you to provide a more detailed explanation about the behavior that you are trying to achieve. Thank you in advance.

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

Regards,
Desislava
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Hugues
Top achievements
Rank 1
answered on 15 Dec 2014, 04:24 PM
This is not quite what i am looking for. I have a class which contains an array of variables. What i want to do is create an Expandable object of all the elements in the array at runtime. So on the top level it would say foo for example and a arrow next to it. When i click the arrow i want to see a drop down of all the elements in my array.

Thanks.
0
Hugues
Top achievements
Rank 1
answered on 16 Dec 2014, 08:04 PM
I have solved it by Inheriting ICustomTypeDescriptor, and create an custom PropertyDescriptor.
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 18 Dec 2014, 02:02 PM
Hello Hugues,

Thank you for writing back.

I am glad that you have found a solution which suits your requirement. I think that the Customized display of collection data in a PropertyGrid Code Project example is quite useful on this topic.

Should you have further questions, I would gladly assist you.

Regards,
Desislava
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
fbi
Top achievements
Rank 1
answered on 14 Dec 2016, 04:36 PM

Hi,

is it possible to get the propertygrid in ConvertTo ?

Best regards

Fritz

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 15 Dec 2016, 11:55 AM
Hello Fritz,

Thank you for writing.  

In order to access the RadPropertyGrid in the ConvertTo, you can achieve it when the ITypeDescriptorContext is PropertyGridItem. Here is a sample code snippet:
public override object ConvertTo(ITypeDescriptorContext context,
    CultureInfo culture,
    object value,
    System.Type destinationType)
{
    PropertyGridItem item = context as PropertyGridItem;
    if (item != null)
    {
        RadPropertyGrid propertyGrid = item.PropertyGridTableElement.ElementTree.Control as RadPropertyGrid;
    }
    Cell cell = value as Cell;
    if (destinationType == typeof(System.String) &&
        value != null)
    {
        return ("X: " + cell.X + " Y: " + cell.Y);
    }
    return base.ConvertTo(context, culture, value, destinationType);
}

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

Regards,
Dess
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
PropertyGrid
Asked by
Hugues
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Hugues
Top achievements
Rank 1
fbi
Top achievements
Rank 1
Share this question
or