1

我有一个文本框:

    <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>

我的 ViewModel 看起来像这样:

  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
}

当我在代码中捕获事件 Validation.Error 时,它的工作原理:

在此处输入图像描述

但是,当我尝试使用 Event Command 以这种方式运行它时,不会出现 Valid 函数。

我错过了什么?

4

2 回答 2

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

于 2013-07-01T10:17:16.560 回答
0

没有Validation.Error事件TextBox。此外,没有Validating事件System.Controls.TextBox(您正在使用)。

用于LostFocus验证文本框或查看此问题,如果您想使用 Validation with MVVM 模式

于 2013-07-01T08:05:31.937 回答