WPF数据验证和显示错误消息IDataErrorInfo和错误模板

时间:2012-09-12 09:11:13

标签: wpf-controls

如何使用IDataErrorInfo和错误模板

对错误进行WPF数据验证并显示错误消息

1 个答案:

答案 0 :(得分:0)

ViewModel:IDataViewModel.cs

using GalaSoft.MvvmLight;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IDataErrorInfo_InputValidation.ViewModel
{
    class IDataViewModel:ViewModelBase,IDataErrorInfo
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int  Age { get; set; }

        public string Error
        {
            get { throw new NotImplementedException(); }
        }

        public string this[string columnName]
        {
            get 
            {
                string result = null;
                if (columnName == "FirstName")
                {
                    if (string.IsNullOrEmpty(FirstName))
                        result = "Please Enter Your FirstName";
                }
                if(columnName == "LastName")
                {
                    if (string.IsNullOrEmpty(LastName))
                        result = "Please Enter Your LastName";
                }
                if(columnName == "Age")
                {
                    if(Age <= 0 || Age >= 99)
                    result ="Please Enter Valid Age";
                }

                return result;
            }
        }
    }
}

xaml:IDataView.xaml

<Window x:Class="IDataErrorInfo_InputValidation.View.IDataView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="IDataView" Height="300" Width="300">
    <Window.Resources>
        <!-- Error template for changing behaviour -->
        <ControlTemplate x:Key="ErrorTemplate">
            <DockPanel LastChildFill="True">
                <Border BorderBrush="Red" BorderThickness="1">
                    <AdornedElementPlaceholder />
                </Border>
            </DockPanel>
        </ControlTemplate>
        <Style TargetType="TextBox">
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="true">
                    <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self},Path=(Validation.Errors)[0].ErrorContent}"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid HorizontalAlignment="Center">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>            
        </Grid.RowDefinitions>
        <TextBox Text="{Binding FirstName,Mode=TwoWay,ValidatesOnDataErrors=True,NotifyOnValidationError=True,ValidatesOnExceptions=True}" Height="20"  Width="100" Margin="10"/>
        <TextBox Text="{Binding LastName,Mode=TwoWay,ValidatesOnDataErrors=True,NotifyOnValidationError=True,ValidatesOnExceptions=True}" Grid.Row="1" Height="20"  Width="100" Margin="10" />
        <TextBox Grid.Row="2" Text="{Binding Age,Mode=TwoWay,ValidatesOnDataErrors=True,NotifyOnValidationError=True,ValidatesOnExceptions=True}" Height="20" Width="100" Margin="10"/>
        <!--<TextBox Grid.Row="2" Height="20"  Width="100" Margin="10"/>-->
        <TextBox Grid.Row="3" Height="20"  Width="100" Margin="10"/>
    </Grid>
</Window>

IDataViewXaml.cs

using System;
using System.Collections.Generic;
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 IDataErrorInfo_InputValidation.View
{
    /// <summary>
    /// Interaction logic for IDataView.xaml
    /// </summary>
    public partial class IDataView : Window
    {
        public IDataView()
        {
            InitializeComponent();
            DataContext = new ViewModel.IDataViewModel();
        }
    }
}