wpf中有validateRule类, 用于界面元素的验证, 如何后台去控制validateRule呢?

1. UI层要binding写好的ValidateRule,分为Binding和MultiBinding, 如下面分别实现了Combobox的SelectedValuePropperty的Binding
     和TextBox的TextProperty的MultiBinding。其中都有ValidationRule。
       <ComboBox x:Name="cmbAgeType" Margin="3"
                      SelectionChanged="cmbAgeType_SelectionChanged"  Background="#00000000" BorderBrush="Black" Grid.Row="4" MinWidth="0" Grid.Column="2" IsTabStop="False" SelectedIndex="0" d:LayoutOverrides="GridBox" Tag="PatientAge"
                      Visibility="{Binding DataContext, ElementName=window, Converter={StaticResource KeyToVisibilityConverter}, ConverterParameter=PatientAge}">
                <ComboBox.SelectedValue>
                    <Binding Path="PatientAge" Converter="{StaticResource AgeMeasureConverter}" Mode="TwoWay"  UpdateSourceTrigger="PropertyChanged">
                        <Binding.ValidationRules>
                            <McsfPAFEContainee_ValidationRules:EmptyValidationRule ValidatesOnTargetUpdated="True" ValidationStep="ConvertedProposedValue"/>
                        </Binding.ValidationRules>
                    </Binding>
                </ComboBox.SelectedValue>
                <!--<Binding Path="PatientAge" Converter=""  UpdateSourceTrigger="PropertyChanged"/>-->
            </ComboBox>
    
            <TextBox x:Name="txtPatientWeight" TextWrapping="Wrap" Margin="3" MaxLength="10" TabIndex="6" BorderBrush="Black" Grid.Row="5" Grid.Column="1" Height="22" MinWidth="42" Tag="PatientWeight"
                     Visibility="{Binding DataContext, ElementName=window, Converter={StaticResource KeyToVisibilityConverter}, ConverterParameter=PatientWeight}">
                <TextBox.Text>
                    <MultiBinding  Mode="TwoWay" Converter="{StaticResource WeightConverter}"   UpdateSourceTrigger="PropertyChanged">
                        <MultiBinding.ValidationRules>
                            <McsfPAFEContainee_ValidationRules:WeightValidationRule ValidatesOnTargetUpdated="True" ValidationStep="ConvertedProposedValue"/>
                        </MultiBinding.ValidationRules>
                        <Binding Path="PatientWeight"/>
                        <Binding Path="IsChecked" ElementName="rdoKg"/>
                    </MultiBinding>
                </TextBox.Text>
                <i:Interaction.Behaviors>
                    <McsfPAFEContainee_Behaviors:NumericTextBoxBehavior MinValue="0" MaxValue="300" />
                </i:Interaction.Behaviors>
            </TextBox>
2.  后台主动触发ValidationRule的验证。 以下方法根据上面的Binding, 分别去取Binding和MultiBinding, 然后调用UpdateSource。
            private bool ValidateInput(object child, PRCfgViewModel vm, bool isUpdateSource, bool isEmergency)
        {
            BindingExpression be = null;
            MultiBindingExpression mbe = null;
            if (child is TextBox)
            {
                be = (child as TextBox).GetBindingExpression(TextBox.TextProperty);
                if (null == be)
                {
                    mbe = BindingOperations.GetMultiBindingExpression((child as TextBox), TextBox.TextProperty);
                }
            }
            else if (child is DatePicker)
            {
                be = (child as DatePicker).GetBindingExpression(DatePicker.TextProperty);
                if (null == be)
                {
                    mbe = BindingOperations.GetMultiBindingExpression((child as DatePicker), DatePicker.TextProperty);
                }
            }
            else if (child is ComboBox)
            {
                be = (child as ComboBox).GetBindingExpression(ComboBox.SelectedValueProperty);
                if (null == be)
                {
                    mbe = BindingOperations.GetMultiBindingExpression((child as ComboBox), ComboBox.SelectedValueProperty);
                }
            }
            if (null == be && null == mbe)
            {
                return false;
            }
            ValidationRule vr = null;
            if (null != be && be.ParentBinding.ValidationRules.Count > 0)
            {
                vr = be.ParentBinding.ValidationRules[0];
            }
            else if (null != mbe && mbe.ParentMultiBinding.ValidationRules.Count > 0)
            {
                vr = mbe.ParentMultiBinding.ValidationRules[0];
            }
            else
            {
                return false;
            }
            string bindingPath = "";
            if (null != be)
            {
                bindingPath = be.ParentBinding.Path.Path;
            }
            else if (null != mbe)
            {
                Binding bd = mbe.ParentMultiBinding.Bindings[0] as Binding;
                bindingPath = bd.Path.Path;
            }
            bindingPath = bindingPath.Replace(".", "_");
            if (vm.Setting.CfgInfo[bindingPath] != null)
            {
                (vr as BaseValidationRule).IsActive = !isEmergency;
                if ((vr as BaseValidationRule).IsActive)
                {
                    (vr as BaseValidationRule).IsAllowEmpty = !(vm.Setting.CfgInfo[bindingPath].IsKeyword);
                }
                else
                {
                    if (isUpdateSource)
                    {
                        if (null != be)
                        {
                            be.UpdateSource();
                        }
                        else if (null != mbe)
                        {
                            mbe.UpdateSource();
                        }
                    }
                    return true;
                }
            }
            else
            {
                (vr as BaseValidationRule).IsAllowEmpty = true;
            }
            if (isUpdateSource)
            {
                if (null != be)
                {
                    be.UpdateSource();
                }
                else if (null != mbe)
                {
                    mbe.UpdateSource();
                }
            }
            return true;
        }

最新文章

  1. BZOJ 1003 物流运输【最短路】【动态规划】
  2. 记AppStore 被打回的经历
  3. 瞎折腾之Mvc WebApi的使用以及跨域问题
  4. easyui源码翻译1.32--Draggable(拖动)
  5. SingleNumber python实现
  6. Angular4.x通过路由守卫进行路由重定向,实现根据条件跳转到相应的页面
  7. 第一个 java 程序
  8. (译)MySQL的10个基本性能技巧
  9. Thinkphp----------Thinkphp3.2的目录结构介绍
  10. python 离散序列 样本数伸缩(原创)
  11. schema举例一
  12. Arduino基本数据类型
  13. Attach()和Detach()函数
  14. Java NIO系列教程(四) Scatter 和 Gather
  15. 简易Python语句获取本机ip地址
  16. python的super用法及含义
  17. lucene学习笔记(四)lucene分词详解
  18. [php]apache的权限解释
  19. linux学习规划
  20. spoj104 highways 生成树计数(矩阵树定理)

热门文章

  1. 基于RedHat发行的Apache Tomcat本地提权漏洞
  2. 卡特兰数-Catalan数
  3. eolinker开源版接口管理
  4. 详解spring boot实现多数据源代码实战
  5. hadoop 出现FATAL conf.Configuration: error parsing conf file,异常
  6. MySQL中使用INNER JOIN来实现Intersect并集操作
  7. 【oracle案例】ORA-01102: cannot mount database in EXCLUSIVE mode
  8. 点聚-weboffice 6.0 (一)
  9. [java,maven] 使用 maven 来搭建简单的 netty 开发环境
  10. 打开蓝牙debug hci log