WPF IDataErrorInfo issue

本文关键字:issue IDataErrorInfo WPF | 更新日期: 2023-09-27 18:28:35

我似乎找不到关于如何设置它的简单解释,有人能帮忙吗?

我几乎读过每一篇教程,每一篇都没有完全解释,我的问题是我已经写了一些代码,但我不确定在MainWindow.xamls.cs中写什么,以及如何进行验证。

public class Person : IDataErrorInfo
    {
        public string Fname { get; set; }
        public string Lname { get; set; }
        public string Error
        {
            get { return ""; }
        }
        public string this[string columnName]
        {
            get
            {
                string result = null;
                if (columnName == "Fname")
                {
                    if (string.IsNullOrEmpty(Fname))
                    {
                        result = "First name is required.";
                        return result;
                    }
                    string st = @"!|@|#|'$|%|'?|'>|'<|'*";
                    if (Regex.IsMatch(Fname, st))
                    {
                        result = "Contains invalid characters.";
                        return result;
                    }
                }
                if (columnName == "Lname")
                {
                    if (string.IsNullOrEmpty(Lname))
                    {
                        result = "Cannot be empty.";
                        return result;
                    }
                }
                return null;
            }
        }
    }

Xaml

<Window x:Class="WpfApplication2.MainWindow"
        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:WpfApplication2"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ControlTemplate x:Key="eTemplate">
            <DockPanel LastChildFill="True">
                <TextBlock DockPanel.Dock="Right" Foreground="Blue" FontSize="13" Text="{Binding ElementName=adorned,Path=AdornedElement.(Validation.Errors)[0].ErrorContent}" >
                </TextBlock>
                <Border BorderBrush="Red" BorderThickness="2">
                    <AdornedElementPlaceholder x:Name="adorned"/>
                </Border>
            </DockPanel>
        </ControlTemplate>
    </Window.Resources>

    <Grid>
        <TextBox Height="23" Validation.ErrorTemplate="{StaticResource ResourceKey=eTemplate}" HorizontalAlignment="Left" Margin="198,71,0,0" Name="Fname" VerticalAlignment="Top" Width="120" FontSize="15">
            <TextBox.Text>
                <Binding Path="Fname" ValidatesOnDataErrors="True" UpdateSourceTrigger="PropertyChanged"/>
            </TextBox.Text>
        </TextBox>
        <TextBox Height="23" Validation.ErrorTemplate="{StaticResource ResourceKey=eTemplate}" HorizontalAlignment="Left" Margin="198,130,0,0" Name="Lname" VerticalAlignment="Top" Width="120" FontSize="15">
            <TextBox.Text>
                <Binding Path="Lname" ValidatesOnDataErrors="True" UpdateSourceTrigger="PropertyChanged"/>
            </TextBox.Text>
        </TextBox>
        <Label Content="FirstName" FontSize="14" Height="28" HorizontalAlignment="Left" Margin="114,71,0,0" Name="FirstName" VerticalAlignment="Top" FontFamily="Consolas" RenderTransformOrigin="0.063,0.607" Width="84"/>
        <Label Content="LastName" FontSize="14" Height="28" HorizontalAlignment="Left" Margin="114,130,0,0" Name="LastName" VerticalAlignment="Top" FontFamily="Consolas" Width="79"/>
        <Button x:Name="Add" Content="test" HorizontalAlignment="Left" Margin="198,186,0,0" VerticalAlignment="Top" Width="120"/>

    </Grid>
</Window>

我下一步该怎么办?

WPF IDataErrorInfo issue

实际上您还没有实现INotifyPropertyChanged接口,因此不会执行属性更改通知。我在你的Person类中做了一些更改,如下所示;

public class Person : IDataErrorInfo, INotifyPropertyChanged
{
    private string _fname;
    private string _lname;
    public String Fname
    {
        get { return _fname; }
        set { _fname = value; OnPropertyChanged("Fname"); }
    }
    public String Lname
    {
        get { return _lname; }
        set { _lname = value; OnPropertyChanged("Lname"); }
    }
    public string Error
    {
        get { return ""; }
    }
    public string this[string columnName]
    {
        get
        {
            string result = null;
            if (columnName == "Fname")
            {
                if (string.IsNullOrEmpty(Fname))
                {
                    result = "First name is required.";
                    return result;
                }
                string st = @"!|@|#|'$|%|'?|'>|'<|'*";
                if (Regex.IsMatch(Fname, st))
                {
                    result = "Contains invalid characters.";
                    return result;
                }
            }
            if (columnName == "Lname")
            {
                if (string.IsNullOrEmpty(Lname))
                {
                    result = "Cannot be empty.";
                    return result;
                }
            }
            return null;
        }
    }
    #region INotifyPropertyChanged implementation
    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(String param)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(param));
        }
    }
    #endregion
}

在MainWindow.cs类中,只需将DataContext设置为Person类;

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new Person();
    }
}