首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在WPF程序中使用IDataErrorInfo.Error?

如何在WPF程序中使用IDataErrorInfo.Error?
EN

Stack Overflow用户
提问于 2012-12-25 00:14:49
回答 2查看 41K关注 0票数 26

我有一个这样的对象:

public class Person : IDataErrorInfo
{
    public string PersonName{get;set;}
    public int Age{get;set;}

    string IDataErrorInfo.this[string propertyName]
    {
        get
        {
            if(propertyName=="PersonName")
            {
                if(PersonName.Length>30 || PersonName.Length<1)
                {
                    return "Name is required and less than 30 characters.";
                }
            }
            return null;
        }
    }

    string IDataErrorInfo.Error
    {
        get
        {
            if(PersonName=="Tom" && Age!=30)
            {
                return "Tom must be 30.";
            }
            return null;
        }
    }
}

绑定PersonName和年龄属性很简单:

<TextBox Text="{Binding PersonName, ValidatesOnDataErrors=True}" />
<TextBox Text="{Binding Age, ValidatesOnDataErrors=True}" />

但是,如何使用Error属性并适当地显示它?

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14023552

复制
相关文章

相似问题

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