Validation.error event in event command

别说谁变了你拦得住时间么 提交于 2019-12-24 13:25:15

问题


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:

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

Did I miss something?


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/17399708/validation-error-event-in-event-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!