Pregunta

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.

enter image description here

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
    }
¿Fue útil?

Solución

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top