企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
wpf中有validateRule类, 用于界面元素的验证, 如何后台去控制validateRule呢? 1. UI层要binding写好的ValidateRule,分为Binding和MultiBinding, 如下面分别实现了Combobox的SelectedValuePropperty的Binding    和TextBox的TextProperty的MultiBinding。其中都有ValidationRule。     <ComboBoxx: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="{BindingDataContext,ElementName=window,Converter={StaticResourceKeyToVisibilityConverter},ConverterParameter=PatientAge}">               <ComboBox.SelectedValue>                   <BindingPath="PatientAge"Converter="{StaticResourceAgeMeasureConverter}"Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">                       <Binding.ValidationRules>                           <McsfPAFEContainee_ValidationRules:EmptyValidationRuleValidatesOnTargetUpdated="True"ValidationStep="ConvertedProposedValue"/>                       </Binding.ValidationRules>                   </Binding>               </ComboBox.SelectedValue>               <!--<Binding Path="PatientAge" Converter=""  UpdateSourceTrigger="PropertyChanged"/>-->           </ComboBox>              <TextBoxx: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="{BindingDataContext,ElementName=window,Converter={StaticResourceKeyToVisibilityConverter},ConverterParameter=PatientWeight}">               <TextBox.Text>                   <MultiBinding Mode="TwoWay"Converter="{StaticResourceWeightConverter}"  UpdateSourceTrigger="PropertyChanged">                       <MultiBinding.ValidationRules>                           <McsfPAFEContainee_ValidationRules:WeightValidationRuleValidatesOnTargetUpdated="True"ValidationStep="ConvertedProposedValue"/>                       </MultiBinding.ValidationRules>                       <BindingPath="PatientWeight"/>                       <BindingPath="IsChecked"ElementName="rdoKg"/>                   </MultiBinding>               </TextBox.Text>               <i:Interaction.Behaviors>                   <McsfPAFEContainee_Behaviors:NumericTextBoxBehaviorMinValue="0"MaxValue="300" />               </i:Interaction.Behaviors>           </TextBox> 2.  后台主动触发ValidationRule的验证。 以下方法根据上面的Binding, 分别去取Binding和MultiBinding, 然后调用UpdateSource。          privateboolValidateInput(objectchild,PRCfgViewModelvm,boolisUpdateSource,boolisEmergency)        {           BindingExpressionbe =null;           MultiBindingExpressionmbe =null;           if(childisTextBox)            {                be = (childasTextBox).GetBindingExpression(TextBox.TextProperty);               if(null== be)                {                    mbe =BindingOperations.GetMultiBindingExpression((childasTextBox),TextBox.TextProperty);                }            }           elseif(childisDatePicker)            {                be = (childasDatePicker).GetBindingExpression(DatePicker.TextProperty);               if(null== be)                {                    mbe =BindingOperations.GetMultiBindingExpression((childasDatePicker),DatePicker.TextProperty);                }            }           elseif(childisComboBox)            {                be = (childasComboBox).GetBindingExpression(ComboBox.SelectedValueProperty);               if(null== be)                {                    mbe =BindingOperations.GetMultiBindingExpression((childasComboBox),ComboBox.SelectedValueProperty);                }            }           if(null== be &&null== mbe)            {               returnfalse;            }           ValidationRulevr =null;           if(null!= be && be.ParentBinding.ValidationRules.Count > 0)            {                vr = be.ParentBinding.ValidationRules[0];            }           elseif(null!= mbe && mbe.ParentMultiBinding.ValidationRules.Count > 0)            {                vr = mbe.ParentMultiBinding.ValidationRules[0];            }           else            {               returnfalse;            }           stringbindingPath ="";           if(null!= be)            {                bindingPath = be.ParentBinding.Path.Path;            }           elseif(null!= mbe)            {               Bindingbd = mbe.ParentMultiBinding.Bindings[0]asBinding;                bindingPath = bd.Path.Path;            }            bindingPath = bindingPath.Replace(".","_");           if(vm.Setting.CfgInfo[bindingPath] !=null)            {                (vrasBaseValidationRule).IsActive = !isEmergency;               if((vrasBaseValidationRule).IsActive)                {                    (vrasBaseValidationRule).IsAllowEmpty = !(vm.Setting.CfgInfo[bindingPath].IsKeyword);                }               else                {                   if(isUpdateSource)                    {                       if(null!= be)                        {                            be.UpdateSource();                        }                       elseif(null!= mbe)                        {                            mbe.UpdateSource();                        }                    }                   returntrue;                }            }           else            {                (vrasBaseValidationRule).IsAllowEmpty =true;            }           if(isUpdateSource)            {               if(null!= be)                {                    be.UpdateSource();                }               elseif(null!= mbe)                {                    mbe.UpdateSource();                }            }           returntrue;        }