Anonymous
Anonymous

Reputation: 9648

Silverlight entity-level validation with MVVM

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, nothing 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.

Upvotes: 0

Views: 599

Answers (1)

Reza F.Rad
Reza F.Rad

Reputation: 230

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.

Upvotes: 2

Related Questions