在setter中验证属性 [英] Validate property in setter

查看:97
本文介绍了在setter中验证属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有财产

 私人  string  _lastName; 
[必填]
[StringLength( 20 )]
public string LastName
{
get { return this ._ lastName;}
set
{
if value != this .- lastName)
{
ValidateProperty( value );
this ._ lastName = value ;
RaisePropertyCanged( LastName);
}
}
}





LastName来自WPF视图中的文本框,两个方式绑定。

当我没有在文本框中填写它时,它是null。代码永远不会到达酒店的制定者。



我尝试过:



尝试将验证方法移出setter。不确定!

解决方案

Maciej Los写道:



你在谈论Model或ViewModel中的验证吗?

会员12658724写道:



ViewModel





因此,您必须向我们展示View与ViewModel的关系。换句话说,NotificationEvent会激活ViewModel ...



此时我建议你阅读一些有趣的文章:

< a href =https://msdn.microsoft.com/en-us/library/hh848246.aspx> MVVM模式 [ ^ ]

MVVM中的Windows验证使用C#for Visual Studio 2012中的数据注释示例 [ ^ ]

WPF中的数据验证| Magnus Montin [ ^ ] - 推荐开始!

验证用户输入 - WPF MVVM [ ^ ]

MVVM数据验证 [ ^ ]

< a href =https://www.tutorialspoint.com/mvvm/mvvm_validations.htm> MVVM验证 [ ^ ]

WPF MVVM验证ViewModel usi ng IDataErrorInfo [ ^ ]


Say I have the property

private string _lastName;
[Required]
[StringLength(20)]
public string LastName
{
      get {return this._lastName;}
      set
      {
           if(value != this.-lastName)
           {
                ValidateProperty(value);
                this._lastName = value;
                RaisePropertyCanged("LastName");
           }
      }
}



The LastName comes from the textbox in a WPF view, two way binding.
When I don't fill it in the textbox, it is null. The code never reaches the setter in property.

What I have tried:

Try to move the Validation method out of setter. Not sure!

解决方案

Maciej Los wrote:


Are you talking about validation in Model or ViewModel?

Member 12658724 wrote:


ViewModel



So, you have to show us how View is related with ViewModel. In other words, what NotificationEvent fires ViewModel...

At this moment i'd recommend you to read few interesting articles:
The MVVM Pattern[^]
Windows Validation in MVVM using Data Annotations sample in C# for Visual Studio 2012[^]
Data validation in WPF | Magnus Montin[^] - recommeded for start!
Validating User Input - WPF MVVM[^]
MVVM Data Validation[^]
MVVM Validations[^]
WPF MVVM Validation ViewModel using IDataErrorInfo[^]


这篇关于在setter中验证属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆