Search code examples
c#winformspropertygrid

How to align popup editor with grid cell?


I have a property grid with a custom editor using a UITypeEditor. I want to align its popup window with the property grid's cell, like the default color editor when the property is a Color, but I can't find any information about the grid cell's location and size.

My UITypeEditor.EditValue method gets a PropertyDescriptorGridEntry object as the context parameter, but it also has no coordinates, and its GridItems collection is empty.

Ideas anybody? Are there (free) alternatives to PropertyGrid that offer this information?

Here is my current code:

class MyPropertyGridEditor : UITypeEditor
{
    public override UITypeEditorEditStyle GetEditStyle( System.ComponentModel.ITypeDescriptorContext context )
    {
        return UITypeEditorEditStyle.Modal;
    }

    // Displays the UI for value selection.
    public override object EditValue( 
                      System.ComponentModel.ITypeDescriptorContext context,
                      System.IServiceProvider provider,
                      object value )
    {
        var form = new MyEditorForm( true );
        // ??? Where can I find Location and Size of the grid cell ???
        if( form.ShowDialog() == DialogResult.OK )
        {
            value = form.Items;
        }

        return value;
    }
}

Example of how my own editor shall be aligned

The above is an example of how I want my form to be aligned, the example shows the default color editor.


Solution

  • The default behavior is keeping the alignment. As also mentioned in the other answer, you are showing a dialog rather than showing a drop down.

    Here is an example to show a simple dropdown. You can show any control as dropdown, in this example, I've shown a ListBox:

    public class MyComponent : Component
    {
        [Editor(typeof(MyUITypeEditor), typeof(UITypeEditor))]
        public string SampleProperty { get; set; }
    }
    
    public class MyUITypeEditor : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }
        IWindowsFormsEditorService svc;
        public override object EditValue(ITypeDescriptorContext context,
            IServiceProvider provider, object value)
        {
            var list = new ListBox();
            var items = new[] { "A", "B", "C" };
            list.Items.AddRange(items);
            list.SelectionMode = SelectionMode.One;
            list.SelectedIndex = 0;
            if (items.Contains(($"{value}")))
                list.SelectedIndex = items.ToList().IndexOf($"{value}");
            list.SelectedValueChanged += List_SelectedValueChanged;
            svc = provider.GetService(typeof(IWindowsFormsEditorService))
                as IWindowsFormsEditorService;
            svc.DropDownControl(list);
            return list.SelectedItem;
        }
    
        private void List_SelectedValueChanged(object sender, EventArgs e)
        {
            svc.CloseDropDown();
        }
    }