zoukankan      html  css  js  c++  java
  • WPF 基础

    1. Binding 对数据的转换和校验

    Binding 中,有检验和转换关卡。

    1.1 数据校验

    源码:

    namespace System.Windows.Data
    {
        public class Binding : BindingBase
        {
            ...
            public Collection<ValidationRule> ValidationRules { get; }    
            ...
        }
    }
    
    namespace System.Windows.Controls
    {
        //
        // 摘要:
        //     提供创建自定义规则的一个方式,旨在检查用户输入的有效性。
        public abstract class ValidationRule
        {
            public abstract ValidationResult Validate(object value, CultureInfo cultureInfo);
        }
        
        //
        // 摘要:
        //     表示 ValidationRule.Validate(Object, CultureInfo)方法返回的结果。
        public class ValidationResult
        {
            public bool IsValid { get; }
            public object ErrorContent { get; }
        }
    }
    

    实例:

    <Slider x:Name="sliderA" Minimum="0" Maximum="100"/>
    <TextBox x:Name="textBoxA"/>
            
    public class RangeValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            double d = 0;
            if (double.TryParse(value.ToString(), out d))
            {
                if (d >= 0 && d <= 10)
                {
                    return new ValidationResult(true, null);
                }
            }
    
            return new ValidationResult(false, "Validation Failed");
        }
    }
    
    RangeValidationRule rvr = new RangeValidationRule();
    
    Binding bindingS = new Binding("Value") { Source = this.sliderA };
    bindingS.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
    bindingS.ValidationRules.Add(rvr);
    
    this.textBoxA.SetBinding(TextBox.TextProperty, bindingS);
    

    拓展:

    1. RangeValidationRule 默认只检验从 Target 回 Source 的数据传递,默认从 Source 传过来的数据是合法的,如果想校验从 Source 传过来的数据,需设置 rvr.ValidatesOnTargetUpdated = true;
    2. 如果想监听校验失败的事件,设置 bindingS.NotifyOnValidationError = true,并为路由事件指定处理程序。

    源码:

    namespace System.Windows.Controls
    {
        public static class Validation
        {
            // 校验失败时触发的事件
            public static readonly RoutedEvent ErrorEvent;
        }
        
        //
        // 摘要:
        //     表示一个验证错误,该错误可通过 System.Windows.Controls.ValidationRule 报告验证错误时由绑定引擎创建
        public class ValidationError
        {
            public object ErrorContent { get; set; }
        }
    }
    

    实例:

    RangeValidationRule rvr = new RangeValidationRule();
    rvr.ValidatesOnTargetUpdated = true;
    
    bindingS.ValidationRules.Add(rvr);
    bindingS.NotifyOnValidationError = true;
    
    this.textBoxA.SetBinding(TextBox.TextProperty, bindingS);       
    this.textBoxA.AddHandler(Validation.ErrorEvent, new RoutedEventHandler(this.ValidationError));
    
    private void ValidationError(object sender, RoutedEventArgs e)
    {
        if (Validation.GetErrors(this.textBoxA).Count > 0)
        {
            MessageBox.Show(Validation.GetErrors(this.textBoxA)[0].ErrorContent.ToString();
        }
    }
    

    1.2 数据转换

    
    namespace System.Windows.Data
    {
        public interface IValueConverter
        {
            // Source To Target
            object Convert(object value, Type targetType, object parameter, CultureInfo culture);
            
            // Target To Source
            object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture);
        }
    }
    
    public enum Category 
    {
        Bomber,
        Fighter
    }
    public class CategoryToSourceConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Category c = (Category)value; 
            switch (c)
            {
                case Category.Bomber:
                    return @"llconsBomber.png"; 
                case Category.Fighter: 
                    return @"lcoosFighter.png";
                default: 
                    return null;
            }
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    
    <Window.Resources>
        <local:CategoryToSourceConverter x:Key="cts"/>
    </Window.Resources>
    
    <Image Widlh="20" Height="20"
           Source="{Binding Path=Category, Converter={StaticResoun cts}}"/>
    
  • 相关阅读:
    【免费】承接各类SharePoint项目开发工作
    数据仓库(一)Introduction
    几种极其隐蔽的XSS注入的防护
    Google SEO优化技术的12个要点总结
    数据结构算法必备
    算法数据结构
    python发邮件
    MySQL数据库中的Date,DateTime,TimeStamp和Time类型
    使用.htaccess实现301重定向
    SharePoint 2010 初次安装时使用向导启动的服务
  • 原文地址:https://www.cnblogs.com/MichaelLoveSna/p/14442848.html
Copyright © 2011-2022 走看看