事件命令中的Validation.error事件 [英] Validation.error event in event command

查看:364
本文介绍了事件命令中的Validation.error事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框:

    <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看起来像这样:

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
}

当我在背后的代码中捕获事件Validation.Error时,它起作用:

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

但是,当我尝试通过Event Command来运行这种方式时,Valid函数无效.

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

我错过了什么吗?

推荐答案

由于Validation.Error附加事件,因此它通常不适用于EventToCommand.

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

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

The answer you will find at the link below:

附有事件的EventToCommand

EventToCommand with attached event

这篇关于事件命令中的Validation.error事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆