1

I have a text box:

    <TextBox Height="20" Width="150" Text="{Binding MyProperty,NotifyOnValidationError=True,ValidatesOnDataErrors=True}" >
          <i:Interaction.Triggers>
          <i:EventTrigger EventName="Validation.Error">
                <mvvm:EventToCommand Command="{Binding MyCmd}" PassEventArgsToCommand="True" ></mvvm:EventToCommand>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </TextBox>

My ViewModel looks like this:

  public class MyViewModel : ValidationViewModelBase, INotifyPropertyChanged
{
    private int myVar;

    [Range(0, 10)]
    public int MyProperty
    {
        get { return myVar; }
        set
        {
            myVar = value;
            OnPropertyChanged("MyProperty");
        }
    }



    public MyViewModel()
    {
        MyCmd = new RelayCommand<RoutedEventArgs>(Valid);
    }

    public RelayCommand<RoutedEventArgs> MyCmd { get; set; }

    private void Valid(RoutedEventArgs args)
    {
        //Do something
    }

    #region INotifyPropertyChanged

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string name)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }

    #endregion INotifyPropertyChanged
}

When I catch the event Validation.Error in Code Behind it works:

enter image description here

But when I try to run it this way with the Event Command is not coming Valid function.

Did I miss something?

Hodaya Shalom
  • 4,327
  • 12
  • 57
  • 111

2 Answers2

2

Since Validation.Error is Attached Event, then it does not work with EventToCommand normally.

The answer you will find at the link below:

EventToCommand with attached event

Community
  • 1
  • 1
Hodaya Shalom
  • 4,327
  • 12
  • 57
  • 111
0

There is no Validation.Error event for TextBox. Furthermore, there is no Validating event for System.Controls.TextBox (which you are using).

Use LostFocus to validate the textbox or see this question if you want to use Validation with MVVM pattern

Community
  • 1
  • 1
Andrey Gordeev
  • 30,606
  • 13
  • 135
  • 162
  • Indeed, there are Validation.Error to a text box. Look at the picture in question. It works in Code Behind. I'm not interested in Lost focus - validation is already working. I want to run command when the validation error occurred. – Hodaya Shalom Jul 01 '13 at 08:10
  • No, there is no `Validation.Error` *event* for `TextBox`. You are trying to use it as *event*, which is wrong. – Andrey Gordeev Jul 01 '13 at 08:15
  • When this in Code Behind - this is not an event? – Hodaya Shalom Jul 01 '13 at 08:28
  • 2
    Validation.Error Attached Event : http://msdn.microsoft.com/en-us/library/system.windows.controls.validation.error(v=vs.100).aspx – Hodaya Shalom Jul 01 '13 at 08:37