1

Sorry for my English,

I have a windows application using MVVM architecture. I have a View with differents controls. One of them is a textbox. I need to validate de user-entered value and set a specific value in the attribute and in a view if the user-entered value is incorrect. I must use the binding property of the textbox. It is not allowed me to use event control...for example PreviewTextInput.

For example, this is my control:

 <TextBox Height="23" Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}"/>

I need In the case that the user enters digit the textbox does not allow this. I need In the case that the user enters negative number set value 1 in the attribute Name and show in the textbox this value. Also, I need if the user enters a value greater of 50 set in the attribute Name 50 and show in the view.

How I can do this?

thanks!

////////////////////////

thank you very much, Raul the problem is that I cant displaying error messages (company policy...) I need to replace the erroneous value with a valid value. For example:

public string Name {

{get { return _name; }

set
{
_name = value;

    if (String.IsNullOrEmpty(value))
    {
       // throw new ApplicationException("Customer name is mandatory."); 

    }
}
}

// Instead of throwing an exception, set in the textbox the numeric value //50...like

if (string.IsNullOrEmpty(tbCasesNumber.Text))
            {
                txtName.text= "50";
            }

How I can do this in WPF?

xyzWty
  • 133
  • 1
  • 14
  • Welcome to SO! As you are new to SO, you might want to read the following help pages: ["How do I ask a good question?"](http://stackoverflow.com/help/how-to-ask) and ["How to create a Minimal, Complete, and Verifiable example"](http://stackoverflow.com/help/mcve) – Jaap May 27 '14 at 11:11

1 Answers1

1

You need to make the validation in your view model's property. I suggest to read Validation in Windows Presentation Foundation, that is an excellent manual, and shows how validation works in WPF.

UPDATE

I found very useful things, first of all:

Greetings ;) and Hope this helps...

Community
  • 1
  • 1
Raúl Otaño
  • 4,640
  • 3
  • 31
  • 65
  • I modified my question. How I can send a value to the view (in this case the textbox) if the user enters a wrong value. Watch the second part of my question please. – user3679316 May 27 '14 at 14:33