gpt4 book ai didi

c# - 如何从 Xaml 在 IDataErrorInfo 验证器中设置属性

转载 作者:太空宇宙 更新时间:2023-11-03 11:51:02 25 4
gpt4 key购买 nike

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

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;
}
}

最佳答案

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

关于c# - 如何从 Xaml 在 IDataErrorInfo 验证器中设置属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2125551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com