3

如何使用 IDataErrorInfo 和错误模板对错误进行 WPF 数据验证并显示错误消息

4

1 回答 1

0

视图模型: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();
        }
    }
}
于 2015-03-09T11:22:49.783 回答