企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
当wpf使用multibinding时, 其内部的validaterule的value 是其多个Binding的值, 要根据情况去验证, 还有就是在做IMultiConverter的ConvertBack时注意TargetType 如: 以下是一个Textbox通过MultiBinding绑定到后台 的字段, 以及前台的一个radiobutton, 该textbox有个validaterule。  <TextBoxx:Name="txtPatientWeight"TextWrapping="Wrap"Margin="3"MaxLength="5"TabIndex="6"BorderBrush="Black"Grid.Row="5"Grid.Column="1"Height="22"MinWidth="42"Tag="PatientWeight"                    Visibility="{BindingDataContext,ElementName=window,Converter={StaticResourceKeyToVisibilityConverter},ConverterParameter=PatientWeight}">               <MultiBinding Mode="TwoWay"Converter="{StaticResourceWeightConverter}"  UpdateSourceTrigger="PropertyChanged">                   <MultiBinding.ValidationRules>                       <McsfPAFEContainee_ValidationRules:WeightValidationRuleValidatesOnTargetUpdated="True"ValidationStep="ConvertedProposedValue"/>                   </MultiBinding.ValidationRules>                   <BindingPath="PatientWeight"/>                   <BindingPath="IsChecked"ElementName="rdoKg"/>               </MultiBinding>               <i:Interaction.Behaviors>                   <McsfPAFEContainee_Behaviors:NumericTextBoxBehaviorMinValue="0"MaxValue="300" />               </i:Interaction.Behaviors>           </TextBox> //////////////////////// <RadioButtonx:Name="rdoKg"Content="kg"GroupName="WeightMeasure"d:LayoutOverrides="GridBox"MinWidth="34"Margin="5,0,0,0"Tag="PatientWeight"HorizontalAlignment="Center">                   <RadioButton.IsChecked>                       <BindingPath="DataContext"Mode="TwoWay"ElementName="window"Converter="{StaticResourceDefaultUnitConverter}"ConverterParameter="kg" UpdateSourceTrigger="PropertyChanged"></Binding>                   </RadioButton.IsChecked>               </RadioButton> [//////////////]()  privatestringpatientWeight="";//(0010,1030) Patient Weight PatientWeight DS unit kg       publicstringPatientWeight        {           get{returnpatientWeight; }           set            {               if(value==patientWeight)                {                   return;                }               patientWeight=value;               OnPropertyChanged("PatientWeight");            }        }   [//////////converter]() publicclassWeightConverter:IMultiValueConverter    {       publicobjectConvert(object[] values,TypetargetType,objectparameter,CultureInfoculture)        {           if(null== values || 2 != values.Length )            {               return"";            }           stringunit ="";           stringtemp = values[0].ToString();           if(String.IsNullOrEmpty(temp))            {               return"";            }           if(0 <= temp.IndexOf("lb"))            {                unit ="lb";            }           elseif(0 <= temp.IndexOf("kg"))            {                unit ="kg";            }           else            {               return"";            }           stringnumber ="";            number = temp.Substring(0, temp.IndexOf(unit));           //KG           if((bool)values[1])            {                measurementUnit ="kg";            }           else//LB            {                measurementUnit ="lb";            }           returnConvertToTargetValue(number, unit, measurementUnit);        }       privatestringmeasurementUnit ="";             publicobject[] ConvertBack(objectvalue,Type[] targetTypes,objectparameter,CultureInfoculture)        {           stringtemp = value.ToString();           if(String.IsNullOrEmpty(temp))            {               returnnewobject[]{DependencyProperty.UnsetValue,DependencyProperty.UnsetValue};            }           if(String.IsNullOrEmpty(measurementUnit))            {                measurementUnit = (PAFEContainee.MainDataContextasMainViewModel).PRCfgVM.IsDefaultKilogram ?"kg":"lb";            }           object[] list =newobject[2];            list[0] = value.ToString() + measurementUnit;            list[1] = measurementUnit.Equals("kg") ?"true":"false";           returnlist;        }       privatestringConvertToTargetValue(stringbefore,stringbeforeUnit,stringafterUnit)        {           stringresult ="";           if(beforeUnit.Equals(afterUnit))            {               returnbefore;            }           switch(afterUnit)            {               case"lb":                    result = ConvertKgToPound(before).ToString();                   break;               case"kg":                    result = ConvertPoundToKg(before).ToString();                   break;               default:                   break;            }           returnresult;        }       privatedoubleConvertPoundToKg(stringpound)        {           doublekg = 0.4536 *double.Parse(pound);           returnkg;        }       privatedoubleConvertKgToPound(stringkg)        {           doublepound = 2.20 *double.Parse(kg);           returnpound;        }    } [///////ValidationRule]() publicoverrideValidationResultDoValidate(objectvalue,CultureInfocultureInfo)        {           try            {               //string field = value.ToString().Trim();               stringfield= (valueasobject[])[0].ToString();               stringpattern=@"^([1-9]|[1-9][0-9]|[1-5][0-9][0-9]|600)((kg)|(lb))$";               Regexreg=newRegex(pattern,RegexOptions.IgnoreCase);               if(field.Length<= 0 ||false==reg.IsMatch(field))                {                   returnnewValidationResult(false,"Please input a number, for example '60'");                }               else                {                   returnValidationResult.ValidResult;                }            }           catch            {               returnnewValidationResult(false,"Patient weight convert failed.");            }        }