当wpf使用multibinding时, 其内部的validaterule的value 是其多个Binding的值, 要根据情况去验证, 还有就是在做IMultiConverter的ConvertBack时注意TargetType

如: 以下是一个Textbox通过MultiBinding绑定到后台 的字段, 以及前台的一个radiobutton, 该textbox有个validaterule。 
 <TextBox x: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="{Binding DataContext, ElementName=window, Converter={StaticResource KeyToVisibilityConverter}, ConverterParameter=PatientWeight}">
                <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>
                <i:Interaction.Behaviors>
                    <McsfPAFEContainee_Behaviors:NumericTextBoxBehavior MinValue="0" MaxValue="300" />
                </i:Interaction.Behaviors>
            </TextBox>
////////////////////////
 <RadioButton x:Name="rdoKg" Content="kg" GroupName="WeightMeasure" d:LayoutOverrides="GridBox" MinWidth="34" Margin="5,0,0,0" Tag="PatientWeight" HorizontalAlignment="Center">
                    <RadioButton.IsChecked>
                        <Binding Path="DataContext" Mode="TwoWay" ElementName="window" Converter="{StaticResource DefaultUnitConverter}" ConverterParameter="kg"  UpdateSourceTrigger="PropertyChanged"></Binding>
                    </RadioButton.IsChecked>
                </RadioButton>
  private string patientWeight = "";//(0010,1030) Patient Weight PatientWeight DS unit kg
        public string PatientWeight
        {
            get { return patientWeight; }
            set
            {
                if (value == patientWeight)
                {
                    return;
                }
                patientWeight = value;
                OnPropertyChanged("PatientWeight");
            }
        }
  
 public class WeightConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (null == values || 2 != values.Length )
            {
                return "";
            }
            string unit = "";
            string temp = values[0].ToString();
            if (String.IsNullOrEmpty(temp))
            {
                return "";
            }
            if (0 <= temp.IndexOf("lb"))
            {
                unit = "lb";
            }
            else if (0 <= temp.IndexOf("kg"))
            {
                unit = "kg";
            }
            else
            {
                return "";
            }
            string number = "";
            number = temp.Substring(0, temp.IndexOf(unit));
            //KG
            if ((bool)values[1])
            {
                measurementUnit = "kg";
            }
            else //LB
            {
                measurementUnit = "lb";
            }
            return ConvertToTargetValue(number, unit, measurementUnit);
        }
        private string measurementUnit = "";
      
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            string temp = value.ToString();
            if (String.IsNullOrEmpty(temp))
            {
                return new object[]{DependencyProperty.UnsetValue, DependencyProperty.UnsetValue};
            }
            if (String.IsNullOrEmpty(measurementUnit))
            {
                measurementUnit = (PAFEContainee.MainDataContext as MainViewModel).PRCfgVM.IsDefaultKilogram ? "kg" : "lb";
            }
            object[] list = new object[2];
            list[0] = value.ToString() + measurementUnit;
            list[1] = measurementUnit.Equals("kg") ? "true" : "false";
            return list;
        }
        private string ConvertToTargetValue(string before, string beforeUnit, string afterUnit)
        {
            string result = "";
            if (beforeUnit.Equals(afterUnit))
            {
                return before;
            }
            switch (afterUnit)
            {
                case "lb":
                    result = ConvertKgToPound(before).ToString();
                    break;
                case "kg":
                    result = ConvertPoundToKg(before).ToString();
                    break;
                default:
                    break;
            }
            return result;
        }
        private double ConvertPoundToKg(string pound)
        {
            double kg = 0.4536 * double.Parse(pound);
            return kg;
        }
        private double ConvertKgToPound(string kg)
        {
            double pound = 2.20 * double.Parse(kg);
            return pound;
        }
    }
 public override ValidationResult DoValidate(object value, CultureInfo cultureInfo)
        {
            try
            {
                //string field = value.ToString().Trim();
                string field = (value as object[])[0].ToString();
                string pattern = @"^([1-9]|[1-9][0-9]|[1-5][0-9][0-9]|600)((kg)|(lb))$";
                Regex reg = new Regex(pattern, RegexOptions.IgnoreCase);
                if (field.Length <= 0 || false == reg.IsMatch(field))
                {
                    return new ValidationResult(false, "Please input a number, for example '60'");
                }
                else
                {
                    return ValidationResult.ValidResult;
                }
            }
            catch
            {
                return new ValidationResult(false, "Patient weight convert failed.");
            }
        }

WPF 使用MultiBinding ,TwoWay ,ValidationRule ,需要注意的事项的更多相关文章

  1. WPF支持OneWay,TwoWay,OneTime,Default和OneWayToSource

    原文:WPF支持OneWay,TwoWay,OneTime,Default和OneWayToSource 无论是目标属性还是源属性,只要发生了更改,TwoWay 就会更新目标属性或源属性. OneWa ...

  2. 【WPF】wpf用MultiBinding解决Converter需要动态传参的问题,以Button为例

    原文:[WPF]wpf用MultiBinding解决Converter需要动态传参的问题,以Button为例       用Binding并通过Converter转换的时候,可能偶尔会遇到传参的问题, ...

  3. 总结:WPF中MultiBinding多值绑定的方法

    原文:总结:WPF中MultiBinding多值绑定的方法 一.Xaml中绑定代码: <TextBlock  Grid.Row="5" Grid.Column="3 ...

  4. WPF Get Multibinding Expression, Update Source,

    wpf 拿到某个control的multibinding以及其中每个Binding 1. 拿到multibinding      MultiBindingExpression mbe = Bindin ...

  5. WPF 之 MultiBinding(多路 Binding)(四)

    一.前言 ​ 有时候 UI 需要显示的信息由不止一个数据来源决定,这时候就需要使用 MultiBinding ,即多路 Binding. ​ MultiBinding 与 Binding 一样均以 B ...

  6. WPF 基础 - MultiBinding

    MultiBinding 与 Binding 一样继承自 BindingBase,因此可以使用 Binding 的地方都可以使用 MultiBinding. namespace System.Wind ...

  7. WPF:自动执行"机器人"程序若干注意事项

    企业应用中,经常会遇到一些需要定时自动执行的程序来完成某些功能,比如:自动定时从第三方web service取回数据.定时对历史数据进行清理.定时向ftp上传业务数据... 这类程序,我习惯称为“机器 ...

  8. WPF关于改变ListBoxItem的颜色的注意事项以及如何找到ListBox中的ItemsPanel

    在ListBox中碰到过几个问题,现在把它写出来: 第一个就是在ListBoxItem中当我用触发器IsSelected和IsMouseOver来设置Background和Foreground的时候, ...

  9. WPF 关于拖拽打开文件的注意事项

    由于开发需求,需要开发一个类似Win图片浏览的工具 当然也涉及到了拖拽打开的需求 按照固有思路: <Grid x:Name="grid1" AllowDrop="T ...

随机推荐

  1. 完好用户体验: 活用window.location与window.open解决页面跳转问题

    原文地址: JavaScript Redirects and window.open原文日期: 2014年08月27日翻译日期: 2014年08月31日翻译人员: 铁锚 (译者注: 本文解决的是按 C ...

  2. js中insertAdjacentHTML的玩法

    原型:insertAdajcentHTML(swhere,stext) insertAdjacentHTML方法:在指定的地方插入html标签语句 参数:swhere: 指定插入html标签语句的地方 ...

  3. AWR元数据的迁移或导入到其它数据库

    我们能够将AWR元数据迁移(导入)到其它数据库.低版本号的导入到高版本号,再用高版本号的数据库生成AWR报告,也能使用一些新特性,如 SQL ordered by Physical Reads (Un ...

  4. 14 nginx 中配置 expires缓存提升网站负载

    一:nginx 中配置 expires缓存提升网站负载 对于网站的图片,尤其是新闻站, 图片一旦发布, 改动的可能是非常小的.我们希望 能否在用户访问一次后, 图片缓存在用户的浏览器端,且时间比较长的 ...

  5. 图像检测之sift and surf---sift中的DOG图 surf hessian

    http://www.cnblogs.com/tornadomeet/archive/2012/08/17/2644903.html http://www.cnblogs.com/slysky/arc ...

  6. ASP.NET动态网站制作(11)-- JQ(3)

    前言:这节课主要是讲CSS作业,然后继续讲jQuery. 内容: 1.css作业讲解. 2.jq设置元素样式:  (1)添加.删除css类别:$("div").addClass(& ...

  7. iOS 逆向 - Class-dump 安装和使用方法

    1.下载安装包 http://stevenygard.com/projects/class-dump/,这里我下载的是 class-dump-3.5.dmp.然后把下载下来的 dmg 打开,复制文件里 ...

  8. leetcode笔记:Pow(x, n)

    一. 题目描写叙述 Implement pow(x, n). 二. 题目分析 实现pow(x, n).即求x的n次幂. 最easy想到的方法就是用递归直接求n个x的乘积,这里须要依据n的值,推断结果是 ...

  9. 基于GeoEvent Processor的物联网应用案例赏析

    1 技术路线 下面全部应用,都採用ArcGIS for Server,结合GeoEvent产品(为一款物联网实时数据集成处理产品)开发完毕. 2 应用场景 1.1   物联网实时态势感知 1.1.1 ...

  10. sap 图标查看

    showicon这个程序很不错,可以显示SAP里所有的ICON(图标). 用事务码SE38直接运行程序:showicon 即可. 显示列表之后,双击任何一个图标可以显示出每一个图标的详细信息.