WPF 数据验证 ExceptionValidationRule

 

 

<Window x:Class="WpfDemo.BindingValidationDemo3"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfDemo"
        mc:Ignorable="d"
        Title="BindingValidationDemo3" Height="300" Width="300">
    <Window.Resources>
        <!--<ControlTemplate x:Key="errorTemp" TargetType="{x:Type TextBox}">
            
        </ControlTemplate>-->
        <Style x:Key="TextBoxStyle" TargetType="{x:Type TextBox}">
            <Setter Property="FontSize" Value="22"/>
            <Setter Property="HorizontalAlignment" Value="Stretch"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="Width" Value="200"/>
            <Setter Property="Height" Value="30"/>

            <!--控制错误信息的显示位置-->
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="true">
                    <Setter Property="Validation.ErrorTemplate">
                        <Setter.Value>
                            <ControlTemplate>
                                <DockPanel>
                                    <TextBlock DockPanel.Dock="Bottom" Foreground="Red" FontSize="13"  
                                Text="{Binding ElementName=errorHint, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
                                    </TextBlock>
                                    <Border BorderBrush="Red" BorderThickness="1">
                                        <AdornedElementPlaceholder Name="errorHint" />
                                    </Border>
                                </DockPanel>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <TextBox x:Name="textBox" Style="{StaticResource TextBoxStyle}" HorizontalAlignment="Left" Margin="45,45,0,0"    VerticalAlignment="Top">
            <TextBox.Text>
                <Binding Path="Name" UpdateSourceTrigger="LostFocus">
                    <Binding.ValidationRules>
                        <ExceptionValidationRule/>
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>
        <TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="23" Margin="89,127,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
    </Grid>
</Window>

  

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 WpfDemo
{
    /// <summary>
    /// BindingValidationDemo3.xaml 的互動邏輯
    /// </summary>
    public partial class BindingValidationDemo3 : Window
    {
        public BindingValidationDemo3()
        {
            InitializeComponent();
            Employee2 e2 = new Employee2();
            textBox.DataContext = e2;
        }
    }

     public class Employee2 : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private string _name;
        public string Name
        {
            set
            {
                _name = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("name"));
                }
                if (string.IsNullOrEmpty(value) || value.Length < 6)
                {
                    throw new ApplicationException("姓名不能为空或者长度不小于6!");
                }
            }
            get { return _name; }
        }
        public int age { set; get; }
    }
}

  

posted @ 2021-07-31 16:25  zq爱生活爱代码  阅读(97)  评论(0编辑  收藏  举报