EventToCommand with attached event

I am using the Validation.Error attached event for a TextBox.

Validation.Error

I want to bind it to EventToCommand .

This usually 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 this, you can see it at the link below:

Bind to mvvm event for command connected event

But I get an error message:

 RoutedEventConverter cannot convert from System.String. 

enter image description here

Can anyone help?

EDIT:

My team in ViewModel

  public MyViewModel() { MyCmd = new RelayCommand<RoutedEventArgs>(Valid); } public RelayCommand<RoutedEventArgs> MyCmd { get; set; } private void Valid(RoutedEventArgs args) { //Do something } 
+4
source share
2 answers

Based on the link you posted,

The RoutedEventTrigger class expects a RoutedEvent , and your RoutedEventTrigger will not be able to convert the Validation.Error string to the required type.

so switch to

 <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 that should be good

+4
source

Thank you for using the RoutedEventTrigger implementation. 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 the code, as @Viv answered. 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 the Grid to contain several CheckBox controls that comes from ToggleButton. The RoutedEvent property is of type "RoutedEvent". I have mirrored the type of RoutedEvent. There is an [TypeConverter] attribute that looks like

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

Maybe this is a new .NET feature, I think :) Anyway THANKS AGAIN!

0
source

Source: https://habr.com/ru/post/1489053/


All Articles