1

I have a UI with some controls in it bound to Person class. Whenever user enters a new information business logic need to check the database if such a person exist. If not I need to give message to user and mark that textbox like it has error(red frame around the box). My Question is can I do that on the getter or setter of the property that gives Validation error?

Thanks for the help!

Dilshod
  • 3,189
  • 3
  • 36
  • 67

3 Answers3

3

using IDataErrorInfo , you can do this as follows,

public class Person : IDataErrorInfo
{
    private int age;

    public int Age
    {
        get { return age; }
        set { age = value; }
    }

    public string Error
    {
        get
        {
            return null;
        }
    }

    public string this[string name]
    {
        get
        {
            string result = null;

            if (name == "Age")
            {
                if (this.age < 0 || this.age > 150)
                {
                    result = "Age must not be less than 0 or greater than 150.";
                }
            }
            return result;
        }
    }
}

in XAML Binding as follows,

<Binding Source="{StaticResource data}" Path="Age"
                    UpdateSourceTrigger="PropertyChanged"
                    ValidatesOnDataErrors="True"   />
Xaruth
  • 4,034
  • 3
  • 19
  • 26
Rajesh Subramanian
  • 6,400
  • 5
  • 29
  • 42
0

I encountered kind of the same problem when learning to use Validations wit WPF I found help through this tutorial, hope it can help you too!

HappyDump
  • 443
  • 9
  • 18
  • -1 It is generally preferable *not* to leave such short answers with just a link in them on StackOverflow. You can find out more in the [Answering section of the Help Center](http://stackoverflow.com/help/how-to-answer). I will remove this downvote if/when you update your answer. – Sheridan Oct 08 '13 at 14:02
  • Thanks for this precision, I'm sorry I did that mistake. – HappyDump Oct 08 '13 at 14:05
  • We all learn something new every day. As I said, if you edit your post by adding some context from your linked tutorial, then I can remove my down vote... I can't physically remove it *until* you make an edit though - it's not possible. – Sheridan Oct 08 '13 at 14:17
  • That's not really what I was talking about, but I'll remove the down vote for your effort. If you take a look at [this post](http://stackoverflow.com/questions/19252276/two-stackpanels-inside-another-stackpanel/19252369?noredirect=1#comment28501374_19252369), you'll see that I have provided a link to a page that could help the question author, but I have also copied some of it into the answer to highlight the relevant part. Please see the [Provide context for links](http://stackoverflow.com/help/how-to-answer_) section of the help pages for more information on this. – Sheridan Oct 09 '13 at 08:02
  • Thank you for the examples and taking the time to help meimprove my posts. – HappyDump Oct 09 '13 at 08:23
  • You're welcome. In StackOverflow, it is customary for experienced users to help newer users with these things, not just with coding matters. – Sheridan Oct 09 '13 at 08:46
0

your VM should implement IDataErrorInfo and set ValidatesOnDataError=True in your Binding. Then you can validate in your ViewModel if such a person exists.

Herm
  • 2,956
  • 19
  • 32