欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

关于WPF使用MultiConverter控制Button状态的详细介绍

程序员文章站 2024-03-03 21:13:34
需求描述1.按钮的状态需要根据多个数据源的内容作出不同的组合判断2.每个数据源的判断规则可定制注:以下功能感觉只是简单粗暴的实现,如果您了解更优雅的解决方案,烦请告诉我下,...

需求描述
1.按钮的状态需要根据多个数据源的内容作出不同的组合判断
2.每个数据源的判断规则可定制
注:以下功能感觉只是简单粗暴的实现,如果您了解更优雅的解决方案,烦请告诉我下,感谢先!

按钮xaml

复制代码 代码如下:

<button name="btnok"
                 grid.column="2"
                 horizontalalignment="left"
                 verticalalignment="center"
                 command="{binding path=okcommand}"
                 content="{dynamicresource common_button_ok}"
                 isdefault="true"
                 style="{dynamicresource buttonstyle}">
           <button.isenabled>
             <multibinding converter="{staticresource invalidmultivalidationruletobooleanmulticonverter}"
               converterparameter="objectnull|greaterthanzerointeger|greaterthanzerointeger|greaterthanzerointeger">
               <binding elementname="comboboxfilter"
                        mode="oneway"
                        path="selecteditem" />
               <binding elementname="textboxframerate"
                        mode="oneway"
                        path="text" />
               <binding elementname="textboxsizewidth"
                        mode="oneway"
                        path="text" />
               <binding elementname="textboxsizeheight"
                        mode="oneway"
                        path="text" />
             </multibinding>
           </button.isenabled>
         </button>

multiconverter判断
复制代码 代码如下:

public class invalidmultivalidationruletobooleanmulticonverter : imultivalueconverter
  {
    #region imultivalueconverter members

    public object convert(object[] values, type targettype, object parameter, cultureinfo culture)
    {
      string[] paramlist = ((string)parameter).split('|');
      if (paramlist == null || paramlist.length <= 0)
      {
        throw new argumentnullexception("parameter");
      }
      int length = paramlist.length;

      ilist<bool> boollist = new list<bool>();

      for (int i = 0; i < paramlist.length; i++)
      {
        switch (paramlist[i].tolowerinvariant())
        {
          case "checknameexisted":
            boollist.add(validationrulehelper.validate<invalidchecknameexistedvalidationrule>(values[i]));
            break;
          case "directoryandfileexist":
            boollist.add(validationrulehelper.validate<invaliddirectoryandfileexistvalidationrule>(values[i]));
            break;
          case "greaterthanzerointeger":
            boollist.add(validationrulehelper.validate<invalidgreaterthanzerointegervalidationrule>(values[i]));
            break;
          case "numericnull":
            boollist.add(validationrulehelper.validate<invalidnumericnullvalidationrule>(values[i]));
            break;
          case "stringlength":
            boollist.add(validationrulehelper.validate<invalidstringlengthvalidationrule>(values[i]));
            break;
          case "stringnullorempty":
            boollist.add(validationrulehelper.validate<invalidstringnulloremptyvalidationrule>(values[i]));
            break;
          case "ipaddress":
            boollist.add(validationrulehelper.validate<invalidipaddressvalidationrule>(values[i]));
            break;
          case "objectnull":
          default:
            boollist.add(validationrulehelper.validate<invalidobjectnullvalidationrule>(values[i]));
            break;
        }
      }

      bool result = boollist[0];
      for (int i = 1; i < boollist.count; i++)
      {
        result = result & boollist[i];
      }

      return result;
    }

    public object[] convertback(object value, type[] targettypes, object parameter, cultureinfo culture)
    {
      return null;
    }

    #endregion
  }