开发者

Silverlight entity-level validation with MVVM

开发者 https://www.devze.com 2023-02-14 01:52 出处:网络
I try to adopt entity-level validation (attributes validation on properties on entities) by create ViewModel that expose that Entity.

I try to adopt entity-level validation (attributes validation on properties on entities) by create ViewModel that expose that Entity.

public class MyViewModel
{
    public MyEntity MyEntity { get; set; }
}

I set binding in xaml to, this xaml page set its DataContext to instance of MyViewModel

TextBlock Text="{Binding MyEntity.MyProperty}"

When I load MyEntity from database and set it to MyViewModel, n开发者_如何学Goothing happen. I also call NotifyPropertyChanged("MyEntity"); and it still nothing happen.

I try again by create MyProperty in MyViewModel

public class MyViewModel
{
    private MyEntity MyEntity { get; set; }

    public string MyProperty 
    {
        get { return this.MyEntity.MyProperty; }
        set { this.MyEntity.MyProperty = value; }
    }
}

And changed xaml to bind to MyProperty. This time when I call NotifyPropertyChanged("MyProperty "); View get update correctly, when I input incorrect data, it has ValidationErrors at MyEntity but View don't raise that error (not show red border)

I want to know how can I get entity-level validation working with MVVM.


Hi
you must change the definition of ViewModel such as

public class MyViewModel:IDataErrorInfo
{
}

and implement interface. this force View to show red border on error.
wish to help.

0

精彩评论

暂无评论...
验证码 换一张
取 消