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

Can't display preview of value in RadPropertyGrid when SelectedObject is ICustomTypeDescriptor

3 Answers 95 Views
PropertyGrid
This is a migrated thread and some comments may be shown as answers.
Dmitriy
Top achievements
Rank 1
Dmitriy asked on 20 Aug 2018, 04:27 PM
Hi!

I am trying to replace standart winforms PropertyGrid, which actually does the job, by RadPropertyGrid but faced up with an issue. Editing object contains string typed property, which stores value of a color, also there is custom editor based on UITypeEditor which should render the value inside property grid. In debug I see that in case of RadPropertyGrid, methods of my editor are not invoked and thus no rendering happens, while everything works ok with standart property grid. How may I make it work with RadPropertyGrid? 

Considering that a code base, I'm working is deep legacy, I tried to create a symplified sample app that demonstrates a use case in our app, described upstairs.
https://github.com/vklu4itesvet/ICustomTypeDescriptor-advantures/tree/master/TelerikPropertyGrid
here is the code 

3 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 21 Aug 2018, 10:18 AM
Hello, Dmitriy,      
 
By design, when a certain property is typeof(Color), RadPropertyGrid creates PropertyGridColorItemElement. However, if the color is stored in a string property, the default visual item element will be used. You can handle the CreateItemElement event and specify what item element to be used. However, note that PropertyGridColorItemElement expects Color type for the value. That is why it would be necessary to convert your string value to a valid Color. Here is a sample code snippet: 

this.radPropertyGrid1.CreateItemElement += RadPropertyGrid1_CreateItemElement;
 this.radPropertyGrid1.SelectedObject = new Foo { Id = 2, SomeColor = Color.Orange.ToArgb().ToString(), AnotherColor = Color.Red.ToArgb()};


private void RadPropertyGrid1_CreateItemElement(object sender, Telerik.WinControls.UI.CreatePropertyGridItemElementEventArgs e)
{
    if (e.Item.Label == "SomeColor")
    {
        e.ItemElementType = typeof(CustoPropertyGridColorItemElement);
    }
}
 
public class CustoPropertyGridColorItemElement : PropertyGridColorItemElement
{
    public ColorEditorColorBox ColorBox
    {
        get
        {
            FieldInfo fi = typeof(PropertyGridColorItemElement).GetField("colorBox", BindingFlags.Instance | BindingFlags.NonPublic);
            return fi.GetValue(this) as ColorEditorColorBox;
        }
    }
 
    public override void Synchronize()
    {
        PropertyGridItem item = (PropertyGridItem)this.Data;
 
        if (item.Value == null || item.Value.Equals(Color.Empty))
        {
            this.ColorBox.BackColor = Color.Empty;
            this.ContentElement.Text = " ";
        }
        else
        {
            if (item.Value is Color)
            {
                this.ColorBox.BackColor = (Color)item.Value;
                this.ContentElement.Text = item.FormattedValue;
            }
            else
            {
            string value = item.Value.ToString();
            this.ColorBox.BackColor = Color.FromArgb(int.Parse(value));
            this.ContentElement.Text = item.FormattedValue;
            }
        }
    }
 
}

I hope this information helps. If you have any additional questions, please let me know.  
 
Regards,
Dess
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Dmitriy
Top achievements
Rank 1
answered on 21 Aug 2018, 10:52 AM
Hello! Thanks for a tip, but in order to get worth migrating 10ths of forms from winforms property grid to telerik - it could be nice to get it working with standart for .net ui mechanism with UITypeEditor and corresponding EditorAttribute. Looks like it is supported by telerik
https://docs.telerik.com/devtools/winforms/propertygrid/attributes#editorattribute
How may I use it, in case when values of my object are delivered by ICustomTypeDescriptor representation?
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 22 Aug 2018, 08:46 AM
Hello, Dmitriy,      

By default, RadPropertyGrid uses a PropertyGridColorEditor for property typeof(Color). However, the editor is activated when the property enters edit mode. The EditorAttribute is respected for the Foo.SomeColor property. If you try to enter edit mode, you will notice that the specified editor is activated. However, the stored value can't be properly converted. As it is demonstrated in the referred Attributes' article, you can change the editor as follows:

[Editor(typeof(PropertyGridColorEditor), typeof(BaseInputEditor))]
public string SomeColor
{
    get
    {
        return _someColor;
    }
 
    set
    {
        _someColor = value;
    }
}



I hope this information helps. If you have any additional questions, please let me know.  
 
Regards,
Dess
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
PropertyGrid
Asked by
Dmitriy
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Dmitriy
Top achievements
Rank 1
Share this question
or