zoukankan      html  css  js  c++  java
  • WPF 验证控件

    WPF 验证控件

    <Window x:Class="WpfApplication2.ValidationTest"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication2"
        Title="ValidationTest" Height="300" Width="800">
        <Window.Resources>
            <Style TargetType="{x:Type TextBox}">
                <Setter Property="Validation.ErrorTemplate">
                    <Setter.Value>
                        <ControlTemplate>
                            <DockPanel LastChildFill="True">
                                <TextBlock DockPanel.Dock="Right"
                                     Foreground="Red"
                                     Margin="5" 
                                     FontSize="12pt"
                                     Text="{Binding ElementName=MyAdorner, 
                                     Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
                                </TextBlock>
    
                                <Border BorderBrush="Green" BorderThickness="3">
                                    <AdornedElementPlaceholder Name="MyAdorner" />
                                </Border>
    
                            </DockPanel>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                    <Trigger Property="Validation.HasError" Value="true">
                        <Setter Property="ToolTip"
                           Value="{Binding RelativeSource={RelativeSource Self}, 
                           Path=(Validation.Errors)[0].ErrorContent}"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
    
        </Window.Resources>
    
    
        <Grid>
            <TextBox Width="200" Height="50" Margin="5" VerticalAlignment="Top">
                <TextBox.Text>
                    <Binding Path="Name" UpdateSourceTrigger="PropertyChanged">
                        <Binding.ValidationRules>
                            <ExceptionValidationRule />
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox.Text>
    
            </TextBox>
    
            <TextBox Width="200" Margin="5" Height="50" VerticalAlignment="Center">
                <TextBox.Text>
                    <Binding Path="Address" UpdateSourceTrigger="PropertyChanged">
                        <Binding.ValidationRules>
                            <local:StringRangeValidationRule MinimumLength="1" MaximumLength="30" 
                             ErrorMessage="Address is required and must be less than 30 letters." />
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox.Text>
            </TextBox>
    
    
            <TextBox Text="HI" Height="50" Width="200" VerticalAlignment="Bottom"/>
    
    
        </Grid>
    </Window>
    

      

    后台代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    
    namespace WpfApplication2
    {
        /// <summary>
        /// Interaction logic for ValidationTest.xaml
        /// </summary>
        public partial class ValidationTest : Window
        {
            public ValidationTest()
            {
                InitializeComponent();
    
                SimpleCustomer c = new SimpleCustomer();
                c.Name = "Fred";
                c.Address = "1/3 Powell Street";
                this.DataContext = c;
    
            }
        }
    
        /// <summary>
        /// 异常验证
        /// </summary>
        public class SimpleCustomer
        {
            public SimpleCustomer()
            { }
    
            private string m_strName;
            public string Name
            {
                set
                {
                    this.m_strName = value;
                    if (String.IsNullOrEmpty(this.m_strName))
                    {
                        throw new ApplicationException("Customer name is mandatory.");
                    }
                }
                get
                {
                    return this.m_strName;
                }
            }
    
            private string m_strAddress;
            public string Address
            {
                set
                {
                    this.m_strAddress = value;
                }
                get
                {
                    return this.m_strAddress;
                }
            }
        }
    
        /// <summary>
        /// 自定义验证
        /// </summary>
        public class StringRangeValidationRule : ValidationRule
        {
            public StringRangeValidationRule()
            { }
    
            private int m_intMinimumLength = -1;
            public int MinimumLength
            {
                set
                {
                    this.m_intMinimumLength = value;
                }
                get
                {
                    return this.m_intMinimumLength;
                }
            }
    
            private int m_intMaximumLength = -1;
            public int MaximumLength
            {
                set
                {
                    this.m_intMaximumLength = value;
                }
                get
                {
                    return this.m_intMaximumLength;
                }
            }
    
            private string m_strErrorMessage = string.Empty;
            public string ErrorMessage
            {
                set
                {
                    this.m_strErrorMessage = value;
                }
                get
                {
                    return this.m_strErrorMessage;
                }
            }
    
    
    
            public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
            {
                ValidationResult result = new ValidationResult(true, null);
                string inputString =(value ??  string.Empty).ToString();
                if ( inputString == null)
                {
                    inputString = string.Empty;
                }
    
                if(inputString.Length < this.m_intMinimumLength || inputString.Length > this.m_intMaximumLength)
                {
                    return new ValidationResult(false, this.ErrorMessage);
                }
                return result;
            }
        }
    
    
    
     
    
    
    }
    

      

  • 相关阅读:
    shell 逻辑操作符
    shell 整数
    shell 字符串
    常用文件测试操作符
    系统级脚本 rpcbind
    shell 特殊字符
    redhat7.5 升级OpenSSH_7.8p1
    kubernetes 项目
    salt 安装kubernetes集群3节点
    web应用
  • 原文地址:https://www.cnblogs.com/linlf03/p/2172466.html
Copyright © 2011-2022 走看看