【问题标题】:How to align popup editor with grid cell?如何将弹出编辑器与网格单元对齐?
【发布时间】:2018-05-21 08:55:48
【问题描述】:

我有一个使用UITypeEditor 的自定义编辑器的属性网格。 我想将其弹出窗口与属性网格的单元格对齐,就像属性为 Color 时的默认颜色编辑器一样,但我找不到有关网格单元格位置和大小的任何信息。

我的UITypeEditor.EditValue 方法得到一个PropertyDescriptorGridEntry 对象作为context 参数,但它也没有坐标,它的GridItems 集合是空的。

有什么想法吗?是否有提供此功能的 PropertyGrid 的(免费)替代品 信息?

这是我当前的代码:

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;
    }
}

以上是我希望我的表单如何对齐的示例,该示例显示了默认的颜色编辑器。

【问题讨论】:

  • 默认行为是保持对齐。我无法重现您遇到的问题。考虑发布minimal reproducible example
  • 在我看来像bike-shedding ????。如果你真的想调整,如何使用DevExpress property grid 之类的东西并在他们的源代码中查看如何挂钩弹出创建过程?或者看看the reference source of the original property grid 以更好地理解并可能加入?
  • 我添加了示例代码并澄清了屏幕截图仅显示了我的表单应如何对齐的示例。颜色编辑器的对齐没有问题,但我希望我自己的编辑器也能这样显示。
  • @Uwe Klein:我想开源我的程序,所以商业产品是没有选择的。正如我上面所说的。
  • 您是否尝试过使用 UITypeEditorEditStyle.DropDown 而不是 Modal,以及 IWindowsFormsEditorService.DropDownControl 而不是显示表单?这就是颜色编辑器的作用。

标签: c# winforms propertygrid


【解决方案1】:

默认行为是保持对齐。正如另一个答案中所提到的,您正在显示一个对话框而不是显示一个下拉菜单。

这是一个显示简单下拉列表的示例。您可以将任何控件显示为下拉菜单,在此示例中,我显示了 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();
    }
}

【讨论】:

    【解决方案2】:

    首先 - 你不显示弹出窗口,芽对话框。这是两个不同的东西。

    PropertyGrid 组件非常复杂,自定义弹出窗口并不像看起来那么容易。

    有颜色编辑器的源代码,你可以从中得到启发。

    http://www.dotnetframework.org/default.aspx/DotNET/DotNET/8@0/untmp/whidbey/REDBITS/ndp/fx/src/Designer/Drawing/System/Drawing/Design/ColorEditor@cs/1/ColorEditor@cs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-31
      • 1970-01-01
      • 2021-06-23
      • 1970-01-01
      • 2018-03-03
      • 1970-01-01
      • 1970-01-01
      • 2018-11-14
      相关资源
      最近更新 更多