EventToCommand with attached event

主宰稳场 提交于 2019-12-10 08:44:02

问题


I use on attached event Validation.Error of the TextBox.

Validation.Error

I want to bind it to EventToCommand.

Normally it does not work:

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

So I found a way to do it, you can see it at the link below:

Attached an mvvm event to command to an attached event

But I get an error:

RoutedEventConverter cannot convert from System.String.

Can anyone help?

EDIT :

My command in the ViewModel

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

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

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

回答1:


Basing it on the link you posted,

The class RoutedEventTrigger expects a RoutedEvent and your xaml is not able to convert the string Validation.Error to the required type.

so switch

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

to

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

and it should be fine




回答2:


Thank you for you sharing the implementation of RoutedEventTrigger. I know this is a very old old post. I tried this solution with VS 2017, in WPF, with MVVM Light 5.4.1. I didn't even need to change code as @Viv replied. Here is my XAML:

    <i:Interaction.Triggers>
        <local:RoutedEventTrigger
            RoutedEvent="ToggleButton.Checked">
            <ml:EventToCommand
                Command="{Binding TestCommand}"
                PassEventArgsToCommand="True" />
        </local:RoutedEventTrigger>
    </i:Interaction.Triggers>

I used a Grid to contain several CheckBox controls, which derives from ToggleButton. The property RoutedEvent is of type "RoutedEvent". I reflected the type RoutedEvent. There is a [TypeConverter] attribute, which looks like

[TypeConverter("System.Windows.Markup.RoutedEventConverter, PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, Custom=null")]

Maybe it's a new .NET feature I guess :) Anyway THANK YOU AGAIN!



来源:https://stackoverflow.com/questions/17401082/eventtocommand-with-attached-event

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