0
votes

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.

1
It would help if you could show us: A) The MyEntity class B) The XAMLMyles J

1 Answers

2
votes

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.