WPF 数据验证和显示错误消息 IDataErrorInfo 和错误模板 [英] WPF data validation and display error message IDataErrorInfo and error templates
本文介绍了WPF 数据验证和显示错误消息 IDataErrorInfo 和错误模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用 IDataErrorInfo 和错误模板对错误进行 WPF 数据验证并显示错误消息
How to do WPF data validation for error and display error message using IDataErrorInfo and error templates
推荐答案
ViewModel: IDataViewModel.cs
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
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
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();
}
}
}
这篇关于WPF 数据验证和显示错误消息 IDataErrorInfo 和错误模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文