首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF IDataErrorInfo问题

WPF IDataErrorInfo问题
EN

Stack Overflow用户
提问于 2015-12-31 09:07:01
回答 1查看 65关注 0票数 2

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

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

班级

代码语言:javascript
复制
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

代码语言:javascript
复制
<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>

我下一步该怎么做?

EN

回答 1

Stack Overflow用户

发布于 2015-12-31 12:33:25

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

代码语言:javascript
复制
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类即可;

代码语言:javascript
复制
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new Person();
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34539663

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档