【问题标题】:How to set property in IDataErrorInfo validator from Xaml如何在 Xaml 的 IDataErrorInfo 验证器中设置属性
【发布时间】:2010-01-24 01:42:52
【问题描述】:

在 WPF 中使用 IDataErrorInfo 时,是否可以将参数传递给验证器。例如,我有一个 DueDate 日期选择器。在验证新任务时,我想将允许的日期限制为今天或以后,但在编辑时我需要允许在今天之前的 DueDates,因为可以编辑过期的任务。

我在 Xaml (.Net 4.0) 中的 DatePicker

<DatePicker SelectedDate="{Binding Path=SelectedIssue.IssDueDate,
            ValidatesOnDataErrors=True}" />

我的 IErrorDataInfo

namespace OITaskManager.Model
{
    public partial class Issue : IDataErrorInfo
    {
    // I want to set these values from the Xaml
    public DateTime minDate = new DateTime(2009, 1, 1);
    public DateTime maxDate = new DateTime(2025, 12, 31);

    public string this[string columnName]
    {
        get
        {
            if (columnName == "IssDueDate")
            {
                if (IssDueDate < minDate || IssDueDate > maxDate)
                {
                    return "Due Date must be later than " + minDate.Date + 
                           " and earlier than " + maxDate.Date;                    
                }
                return null;
            }
            return null;
        }
    }

【问题讨论】:

    标签: c# wpf validation xaml idataerrorinfo


    【解决方案1】:

    您可以在绑定上使用custom validator。或者,您可以在 Issue 对象实例上维护 IsNew 内部状态,直到它不再被视为新对象。

    【讨论】:

    • 为什么总是有 7 种方法可以做任何事情?我对此进行了一些研究,看起来这将起作用。我的模型是 Linq to SQL 数据集,并且我添加了带有验证规则的部分类,这些规则实现了 IDataErrorInfo。如果我对日期使用自定义验证器,我应该保留 IDataErrorInfo 验证,还是应该将检查添加到自定义日期验证器以确保最小和最大日期不在允许的范围之外。后者似乎最简单,但在两个地方进行数据库验证感觉很糟糕。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多