silverlight MVVM 中的验证错误消息
我正在尝试从 Josh Smith 的示例中实现数据验证技术: 使用视图模型提供有意义的验证...
我的代码与示例非常相似,除了一些区别,即我正在使用 MVVM-轻型工具包,我的模型人员类是来自 WCF 后端的部分类。
这是有问题的代码的示例:
首先是来自 WCF 的类的自动生成版本:
public partial class Person : BaseObject
{
private string FooField;
public string Foo {
get {
return this.FooField;
}
set {
if ((object.ReferenceEquals(this.FooField, value) != true)) {
this.FooField = value;
this.RaisePropertyChanged("Foo");
}
}
}
然后我扩展分部类以实现 IDataErrorInfo:
public partial class Person : IDataErrorInfo
{
public string Error
{
get { return null;}
}
public string this[string propertyName]
{
if (propertyName == "Foo")
{
//Do some backend Validation
}
}
}
最后我有一个视图模型:
public class PersonViewModel : INotifyProperyChanged, IDataErrorInfo
{
private string _fooString;
private Person _person;
...
public string Foo {
get { return _fooString; }
set
{
if (value == _fooString;)
return;
_fooString = value;
RaisePropertyChanged("Foo");
}
public string this[string propertyName]
{
if (propertyName == "Foo")
{
string msg = Validate(Foo); //Frontend Validation, range, format, etc.
if(msg ! = null)
return msg;
_person.Foo = Foo;
}
}
}
所以当我绑定到viewmodel IDataErrorInfo 索引器中定义的验证代码将在视图模型和我的文本框上执行,如果我的验证失败,则会按预期突出显示任何内容。然而,在我的代码中,模型端的索引器根本不会被执行。我可以诚实地说,我没有看到或理解应该调用它的机制。我已经运行了 Josh Smith 示例中的示例代码,它确实有效,调用了 MV 的 this[],然后如果验证通过了模型的 this[] 则进行额外验证,但我一生都看不出它是如何实现的发生。
我真的希望这是我忽略的简单事情。感谢您查看它。
I'm attempting to implement the technique for data validation from Josh Smith's example here: Using a viewmodel to provide meaningful validation...
My code is remarkably similar to the example, except for a few difference, namely I'm using the MVVM-Light toolkit, and my model person class is a partial class that comes from a WCF backend.
Here's an example of the code in question:
First is the Automatically generated version of the class which comes from the WCF:
public partial class Person : BaseObject
{
private string FooField;
public string Foo {
get {
return this.FooField;
}
set {
if ((object.ReferenceEquals(this.FooField, value) != true)) {
this.FooField = value;
this.RaisePropertyChanged("Foo");
}
}
}
I then extend the partial class to implement IDataErrorInfo:
public partial class Person : IDataErrorInfo
{
public string Error
{
get { return null;}
}
public string this[string propertyName]
{
if (propertyName == "Foo")
{
//Do some backend Validation
}
}
}
And lastly I have a viewmodel:
public class PersonViewModel : INotifyProperyChanged, IDataErrorInfo
{
private string _fooString;
private Person _person;
...
public string Foo {
get { return _fooString; }
set
{
if (value == _fooString;)
return;
_fooString = value;
RaisePropertyChanged("Foo");
}
public string this[string propertyName]
{
if (propertyName == "Foo")
{
string msg = Validate(Foo); //Frontend Validation, range, format, etc.
if(msg ! = null)
return msg;
_person.Foo = Foo;
}
}
}
So when I bind to the property in the viewmodel the validation code defined in IDataErrorInfo's indexer gets executed on the view model and my textbox or whatever gets highlighted if my validation fails, as expected. However in my code the Indexer on the MODEL side never gets executed, at all. I can honestly say that I don't see or understand the mechanism that is supposed to invoke it. I have run the example code from Josh Smith's example, and it does work, calling the MV's this[], then if validation passes the Model's this[] hits for additional validation, but I for the life of me can't see how it happens.
I really hope this is something simple I'm overlooking. Thanks for looking at it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您缺少
ViewModel 中的索引器。
You are missing
from the the indexer in the ViewModel.