开发者社区> zting科技> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

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;
        }
    }
 
 
 
  
 
 
}

  


本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/archive/2011/12/26/2172466.html,如需转载请自行联系原作者

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
WPF自定义控件09:FlatWaveButton
按钮在很多应用程序中都是必不可少的控件,如果给按钮控件添加一些动画效果,比如单击它后会显示一个水波纹扩散的动画效果,那么感觉上更加的高级。本文将介绍一下自定义水波纹按钮控件FlatWaveButton,它是一个UserControl(FlatButton)控件,自身携带UI样式和后台逻辑。
0 0
WPF 控件自定义背景
<!--控件要设置尺寸的话,设置的尺寸必须比下面的图形的尺寸要小,不然显示不开--> <Label Content="直角测试" Width="90" Height="90" HorizontalContentAlignment="Center" Vert...
618 0
WPF范围选择控件(RangeSelector)
原文:WPF范围选择控件(RangeSelector) 版权声明:本文为博主原创文章,转载请注明作者和出处 https://blog.csdn.net/ZZZWWWPPP11199988899/article/details/78084886        在某些应用场景中,我们需要做可视化的范围选择。
509 0
在WPF中使用WinForm控件方法
原文:在WPF中使用WinForm控件方法 1、      首先添加对如下两个dll文件的引用:WindowsFormsIntegration.dll,System.Windows.Forms.dll。
592 0
在Winform窗体中使用WPF控件(附源码)
原文:在Winform窗体中使用WPF控件(附源码) 今天是礼拜6,下雨,没有外出,闲暇就写一篇博文讲下如何在Winform中使用WPF控件。原有是我在百度上搜索相关信息无果,遂干脆动手自己实现。 WPF控件的漂亮是Winform无法匹及的,本文主旨是在Winform工程中如何使用WPF控件。
627 0
WPF 控件 深度克隆
原文:WPF 控件 深度克隆 http://social.msdn.microsoft.com/Forums/zh-SG/wpfzhchs/thread/e5c87129-966a-4d51-a934-ccb8fc7620ec   比如 canvas里面有一个RichTextBox1 ,我现在要...
673 0
WPF控件
转自:http://blog.csdn.net/lisenyang/article/details/18312067 程序的本质就是“数据+算法”------用户输入原始的数据,算法处理原始数据并得到结果数据。
1119 0
+关注
文章
问答
文章排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载