验证WPF和SL中的依赖属性
验证两个属性的推荐方法是什么 经典示例是开始日期应低于结束日期:验证WPF和SL中的依赖属性,wpf,silverlight,validation,reactiveui,Wpf,Silverlight,Validation,Reactiveui,验证两个属性的推荐方法是什么 经典示例是开始日期应低于结束日期: 用户输入开始“第6个” 用户输入结束“第三”-这两个字段都应标记为无效 用户将“开始”改为“第一”-两个字段都应正常 ReactiveValidatedObject如何在这里提供帮助 我最好需要一个能在WPF和Silverlight中工作的解决方案。如果您在WPF应用程序中使用MVVM模式,它将非常简单。取而代之的是视图进行验证,取而代之的是ViewModel。 视图应该只是一个哑层,显示ViewModel公开的任何内容。所有UI
我最好需要一个能在WPF和Silverlight中工作的解决方案。如果您在WPF应用程序中使用MVVM模式,它将非常简单。取而代之的是视图进行验证,取而代之的是ViewModel。 视图应该只是一个哑层,显示ViewModel公开的任何内容。所有UI验证都应该由ViewModel完成,以便它们是可测试的 我的ViewModel可能如下所示:
class MyViewModel : INotifyPropertyChanged
{
/* declare ProperChanged event and implement OnPropertyChanged() method */
private DateTime _firstDate;
public DateTime FirstDate
{
get { return _firstDate; }
set
{
if (!AreDatesValid(value, _secondDate))
{
ErrorMessage = "Incorrect First Date";
return;
}
_firstDate = value;
OnPropertyChanged("FirstDate");
}
}
private DateTime _secondDate;
public DateTime SecondDate
{
get { return _secondDate; }
set
{
if (!AreDatesValid(_firstDate, value))
{
ErrorMessage = "Incorrect Second Date";
return;
}
_secondDate = value;
OnPropertyChanged("SecondDate");
}
}
private string _errorMessage;
public string ErrorMessage
{
get { return _errorMessage; }
set
{
_errorMessage = value;
OnPropertyChanged("ErrorMessage");
}
}
private bool AreDatesValid(DateTime firstDate, DateTime secondDate)
{
if(firstDate <= secondDate )
return true;
return false;
}
}
类MyViewModel:INotifyPropertyChanged
{
/*声明PropertyChanged事件并实现OnPropertyChanged()方法*/
私有日期时间_firstDate;
公共日期时间第一日期
{
获取{return\u firstDate;}
设置
{
如果(!AreDatesValid(值,_secondDate))
{
ErrorMessage=“第一次日期不正确”;
返回;
}
_firstDate=值;
不动产变更(“首次日期”);
}
}
私有日期时间_secondDate;
公共日期时间秒日期
{
获取{return\u secondDate;}
设置
{
如果(!AreDatesValid(_firstDate,value))
{
ErrorMessage=“第二个日期不正确”;
返回;
}
_第二个日期=值;
不动产变更(“第二日期”);
}
}
私有字符串错误消息;
公共字符串错误消息
{
获取{return\u errorMessage;}
设置
{
_errorMessage=值;
OnPropertyChanged(“错误消息”);
}
}
私有bool AreDatesValid(DateTime firstDate,DateTime secondDate)
{
如果(第一个日期)
我知道如何在Silverlight中使用INotifyDataErrorInfo
界面执行此操作:如果更改了两个属性的一个相关属性-fireErrorsChanged
事件。但我不知道如何在WPF中执行此操作。
<DataTemplate DataType="{x:Type ViewModel:MyViewModel}">
<Grid>
<TextBox Text="{Binding Path=FirstDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<TextBox Text="{Binding Path=SecondDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<TextBlock Text="{Binding Path=ErrorMessage}" />
</Grid>
<DataTemplate>