2020-11-22

业务需求:

业务工作中经常需要对表格中的数据进行处理,包括过滤、复合计算等。过滤需要有过滤条件,复合计算需要计算公式。这两种场景都需要一个表达式编辑器。GridControl自带过滤条件的表达式编辑器,我们要做的就是把这个编辑器拿出来,独立于GridControl,进而可以绑定到其它控件上。

实现原理:

找到表达式编辑器内部类UnboundColumnExpressionEditorForm,这是一个窗口类。我们将其边框设置为None,Dock属性设置为Fill,拖放到控件上,使其看上去像个控件,并公布出获取表达式字符串的方法。

效果图:

2020-11-22

代码片段:

public class UnboundExpressionPanel : PanelControl {
      public UnboundExpressionPanel() : base() {
          BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
          StartEdit(new GridColumn()); 
      }
 
      object[] arguments;
      protected object[] Arguments { get { return arguments; } }
 
      MyUnboundColumnExpressionEditorForm form = null;
      protected MyUnboundColumnExpressionEditorForm Form { get { return form; } }
 
      private GridColumn fUnboundColumn;
      public GridColumn UnboundColumn {
          get { return fUnboundColumn; }
          set {
              if (fUnboundColumn == value) return;
              StartEdit(value);
          }
      }
 
      protected MyUnboundColumnExpressionEditorForm CreateForm(params object[] arguments)
      {
          return new MyUnboundColumnExpressionEditorForm(arguments[0], null);
      }
 
      protected void ApplyExpression(string expression) {
          if (Arguments == null) return;
          ((GridColumn)Arguments[0]).UnboundExpression = expression;
      }
 
      public void StartEdit(params object[] arguments) {
          if (arguments.Length < 1) return;
          GridColumn unboundColumn = arguments[0] as GridColumn;
          if (unboundColumn == null) return;
          fUnboundColumn = unboundColumn;
          DestroyForm();
          this.arguments = arguments;
          this.form = CreateForm(arguments);
          if (form == null) return;
          form.Dock = DockStyle.Fill;
          form.TopLevel = false;
          form.FormBorderStyle = FormBorderStyle.None;
          form.Closing += new CancelEventHandler(form_Closing);
          form.buttonCancel.Click += new EventHandler(buttonCancel_Click);
          form.buttonOK.Text = "Apply";
          Controls.Add(form);
          form.Visible = true;
      }
 
      void buttonCancel_Click(object sender, EventArgs e)
      {
          if (form != null) form.Close();
      }
 
      void form_Closing(object sender, CancelEventArgs e)
      {
          e.Cancel = true;
          if (this.arguments == null || this.form == null) return;
          if (form.DialogResult == DialogResult.OK)
          {
              ApplyExpression(form.Expression);
          }
          else
          {
              form.ResetMemoText();
          }
      }
 
      public void DestroyForm()
      {
          if (form != null) form.Dispose();
          form = null;
      }
  }
 
  public class MyUnboundColumnExpressionEditorForm : UnboundColumnExpressionEditorForm
  {
      public MyUnboundColumnExpressionEditorForm(object contextInstance, 
          IDesignerHost designerHost) 
          : base(contextInstance, designerHost) { }
 
      private string GetExpressionMemoEditText()
      {
          GridColumn column = ContextInstance as GridColumn;
          return null == column ? string.Empty : column.UnboundExpression;
      }
 
      public void ResetMemoText()
      {
          ExpressionMemoEdit.Text = GetExpressionMemoEditText();
      }
  }

 

 

 

 

 

 

上一篇:函数


下一篇:箭头函数和普通函数的区别