• DevExpress控件扩展之表达式编辑器


    业务需求:

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

    实现原理:

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

    代码片段:

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

    下载地址:

    http://pan.baidu.com/s/1sjn24cH

  • 相关阅读:
    iOS 获取当前设备类
    iOS 字体详解
    iOS屏幕旋转总结
    CocoaPods安装和使用教程
    【iOS】build diff: /../Podfile.lock: No such file or directory
    Eclipse使用Maven构建web项目
    UIScrollView增加刷新
    Mac下打开eclipse 始终提示 你需要安装Java SE 6 Runtime
    嵌入支付宝SDK,出现“LaunchServices: ERROR: There is no registered handler for URL scheme alipay”错误
    iOS开发 传感器(加速计、摇一摇、计步器)
  • 原文地址:https://www.cnblogs.com/njuxdj/p/4598968.html
Copyright © 2020-2023  润新知