zoukankan      html  css  js  c++  java
  • WPF:解决数据验证ValidationRule与按钮Canexcute联动的问题

    想法是这样的,
    如果用户输入有误,界面上显示ValidationRule的提示,并且按钮enabled=false(canExcute=fales)
    按钮是在输入信息属性改变时刷新Canexcute方法

    实际中validRule:RequiredNumberValidRule ValidatesOnTargetUpdated="True"默认是先验证再赋值,如果验证不通过就不赋值,
    这就造成了如果验证不通过不能执行Canexcute而按钮无法enabled=false

    此时需要更改ValidationRule的属性,设置ValidationStep="UpdatedValue"或者CommiteVALUE

    此时还是不行,设置ValidationStep="UpdatedValue后,绑定的属性就变成了绑定的表达式了,
    参考:https://stackoverflow.com/questions/10342715/validationrule-with-validationstep-updatedvalue-is-called-with-bindingexpressi
    改写了下代码,成功!

    7.31更新 实际中发现第一版的GetBoundValue有问题,不能获取嵌套属性的属性值(A.B.C),遂又找了一个方法:
    https://stackoverrun.com/ja/q/2731242
    (PS:感谢大学思修的开卷考试,让我练就了一手快速翻书能力)

    ····

    private object GetBoundValue(object value)
        {
            if (value is BindingExpression)
            {
                // ValidationStep was UpdatedValue or CommittedValue (validate after setting)
                // Need to pull the value out of the BindingExpression.
                BindingExpression binding = (BindingExpression)value;
    
                // Get the bound object and name of the property
                string resolvedPropertyName = binding.GetType().GetProperty("ResolvedSourcePropertyName", BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance).GetValue(binding, null).ToString();
    
                object resolvedSource = binding.GetType().GetProperty("ResolvedSource", BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance).GetValue(binding, null);
    
                // Extract the value of the property
                object propertyValue = resolvedSource.GetType().GetProperty(resolvedPropertyName).GetValue(resolvedSource, null);
    
                return propertyValue;
            }
            else
            {
                return value;
            }
        }
    

    ····

    原答案
    ·---------------------------------------------------------------------------------------------------------------------------------

    ····

     <TextBox  Grid.Column="2" Height="26"  >
                        <Binding Path="Compare_Correlation" UpdateSourceTrigger="PropertyChanged">
                            <Binding.ValidationRules>
                                <validRule:RequiredNumberValidRule ValidatesOnTargetUpdated="True" ValidationStep="UpdatedValue" />
                            </Binding.ValidationRules>
                        </Binding>
                    </TextBox>
    
    
    public class RequiredNumberValidRule : ValidationRule
       {
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            if (GetBoundValue(value) is null)
            {
                return new ValidationResult(false, "required field.");
            }
            var temp = GetBoundValue(value).ToString();
            double doubleValue;
            if (double.TryParse(temp, out doubleValue))
            {
                return new ValidationResult(true, null);
            }
            else
            {
                return new ValidationResult(false, "Incorrect number format.");
            }
        }
    
        private object GetBoundValue(object value)
        {
            if (value is BindingExpression)
            {
                // ValidationStep was UpdatedValue or CommittedValue (Validate after setting)
                // Need to pull the value out of the BindingExpression.
                BindingExpression binding = (BindingExpression)value;
    
                // Get the bound object and name of the property
                object dataItem = binding.DataItem;
                string propertyName = binding.ParentBinding.Path.Path;
    
                // Extract the value of the property.
                object propertyValue = dataItem.GetType().GetProperty(propertyName).GetValue(dataItem, null);
    
                // This is what we want.
                return propertyValue;
            }
            else
            {
                // ValidationStep was RawProposedValue or ConvertedProposedValue
                // The argument is already what we want!
                return value;
            }
        }
    }
    

    ····

  • 相关阅读:
    网络卖家自曝黑幕 “信用刷手”欺骗你 狼人:
    卡巴斯基:2009年恶意软件发展情况将持续恶化 狼人:
    微软证实云计算平台暂时中断 显现安全弊端 狼人:
    工程目录Maven安装配置及其插件m2e(Eclipse Indigo 和 MyEclipse8.5)的安装配置
    文件系统crondsendmailpostdrop导致Linux定期死掉的完整解决过程实录
    汇编语言处理器面向机器的编程语言 : 汇编语言:如果自己要发明一种编程语言,那么需要做些什么事情呢?
    包解密腾讯手机管家ROOT功能分析
    nullnullhdu 2608
    按钮下载Eclipse Color Theme
    控件学习IOS开源项目(1)之RatingView星级评论控件学习
  • 原文地址:https://www.cnblogs.com/swobble/p/13391448.html
Copyright © 2011-2022 走看看