c#自定义UITypeEditor在按确定和取消后不触发

时间:2018-08-10 07:26:41

标签: c# winforms uitypeeditor

我有一个Form,允许用户自定义Chart,就像更改ColorLegendText等一样。我在下面实现了一个自定义UIEditor:

class MySeriesCollectionTypeEditor : UITypeEditor
{        
    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
    {
        return UITypeEditorEditStyle.Modal;
    }
    public override object EditValue(ITypeDescriptorContext context, System.IServiceProvider provider, object value)
    {
        IWindowsFormsEditorService svc = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
        if (svc != null)
        {
            Series[] oldSeriesCollection = new Series[] { };
            oldSeriesCollection = (Series[])value;

            using (MyChartSeriesEditor frm = new MyChartSeriesEditor((Series[])value))
            {
                if (svc.ShowDialog(frm) == DialogResult.OK)
                {
                    return frm.SeriesCollections;
                }
                else
                {
                    return oldSeriesCollection;
                }
            }
        }
        return value;
    }
}

在我的MyChartSeriesEditor上:

public Series[] SeriesCollections { get; set; }
public OptChartSeriesEditor(Series[] _seriesCollections)
{
   InitializeComponent();
   btnOK.DialogResult = DialogResult.OK;
   btnCancel.DialogResult = DialogResult.Cancel;
}

然后选择属性:

[Browsable(true)]
[Category("Series")]
[DisplayName("SeriesCollections")]
[Editor(typeof(MySeriesCollectionTypeEditor), typeof(UITypeEditor))]
public Series[] SeriesCollections
{
   get { return _SeriesCollections; }
   set
   {
     _SeriesCollections = value;
     MessageBox.Show("Test Message!");        
   }
}

当我单击Ellipsis时,它会显示Form,但是当我单击OkCancel时,Form只是关闭而没有任何反应

1 个答案:

答案 0 :(得分:0)

尝试这种方法

    public Series[] SeriesCollections { get; set; }
    public OptChartSeriesEditor(Series[] _seriesCollections)
    {
       InitializeComponent();
       btnOK.Click += (source, e) =>
       {
           //do some saving or other logic required after ok
           this.DialogResult = DialogResult.OK;
           this.Close();
       };
       btnCancel.Click += (source, e) => 
       {
           this.DialogResult = DialogResult.Cancel;
           this.Close();
       };
    }