EventToCommand与附加事件

4

我使用TextBox的附加事件Validation.Error

Validation.Error

我想将其绑定到 EventToCommand

通常它不起作用:

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

我找到了一种方法来实现它,你可以在下面的链接中看到:

将MVVM事件附加到命令附加事件

但是我遇到了一个错误:

RoutedEventConverter cannot convert from System.String.

enter image description here

请问有人可以帮忙吗?

编辑:

在我的ViewModel中的命令

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

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

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

你的 ViemModel "MyCmd" 命令的定义是什么? - Bruno
1个回答

4
根据你发布的链接,类“RoutedEventTrigger”需要一个“RoutedEvent”,但是你的XAML无法将字符串“Validation.Error”转换为所需类型。因此,请切换。
<i:Interaction.Triggers>
  <view_model:RoutedEventTrigger RoutedEvent="Validation.Error">
    <mvvm:EventToCommand Command="{Binding MyCmd}" PassEventArgsToCommand="True" />
  </view_model:RoutedEventTrigger>
</i:Interaction.Triggers>

为了

<i:Interaction.Triggers>
  <view_model:RoutedEventTrigger RoutedEvent="{x:Static Validation.ErrorEvent}">
    <mvvm:EventToCommand Command="{Binding MyCmd}" PassEventArgsToCommand="True" />
  </view_model:RoutedEventTrigger>
</i:Interaction.Triggers>

并且它应该没问题。


什么是 view_model: ?? - Allan Ruin
@AllanRuin 只需要命名空间,该命名空间声明了来自原始帖子中链接文章的 RoutedEventTrigger 类。 - Viv

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接