wpf - 事件命令中的Validation.error事件

标签 wpf events mvvm binding eventtocommand

我有一个文本框:

    <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时,它起作用:

但是,当我尝试使用“事件命令”以这种方式运行它时,没有提供“有效”功能。

我错过了什么?

最佳答案

由于Validation.Error附加事件,因此它通常无法与EventToCommand一起使用。

您将在以下链接中找到答案:

EventToCommand with attached event

关于wpf - 事件命令中的Validation.error事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17399708/

相关文章:

c# - 如何在 MVVM 中绑定(bind) Xtended WPF 工具包向导的 CurrentPage?

WPF C# 如何在代码中创建 THIS 绑定(bind)?

WPF:DatePicker 文本垂直居中

events - 用于连续查询的 Cassandra(+ Spark?)的最佳方法?

perl - 如何使我的 Perl Jabber 机器人成为事件驱动程序?

java - 使用MVVM和LiveData登录

mvvm - ViewModel 到 ViewModel 的通信

c# - 仅绑定(bind)日期和时间

wpf - ItemsControl 在 MainWindow 的构造函数中没有子项

javascript - jquery on() 不能处理点击事件?