EventToCommand with Textbox GotFocus event results in an exception

Feb 28, 2010 at 2:11 AM
Edited Feb 28, 2010 at 2:13 AM

 

Hello,

I am trying to use MVVM Light V3/alpha 3 in a SilverLight 4 project to bind a TextBox's GotFocus event to a RelayCommand in my ViewModel.  I started by exposing HandleFocusCommand on my VM:

public RelayCommand<string> HandleFocusCommand
{
	get;
	private set;
}

Then I set the Handler in my VM's constructor:

public SearchViewModel()
{
	HandleFocusCommand = new RelayCommand<string>(e =>
	{
		if (query == DefaultSearchLabel)
			Query = String.Empty;
	});
}

In my xaml, I setup the event trigger on my TextBox:

<TextBox Text="{Binding Query, Mode=TwoWay}" Margin="15,10,15,10" TextWrapping="Wrap" VerticalAlignment="Top" d:LayoutOverrides="Width">
	<i:Interaction.Triggers>
		<i:EventTrigger EventName="GotFocus">
			<cmd:EventToCommand  Command="{Binding HandleFocusCommand, Mode=OneWay}"/>
		</i:EventTrigger>
	</i:Interaction.Triggers>
</TextBox>

This compiles, but at runtime I get the following DataBinding exception:

System.Windows.Data Error: 'MS.Internal.Data.DynamicValueConverter' converter failed to convert value 'GalaSoft.MvvmLight.Command.RelayCommand`1[System.Windows.RoutedEventArgs]' (type 'GalaSoft.MvvmLight.Command.RelayCommand`1[System.Windows.RoutedEventArgs]'); BindingExpression: Path='HandleFocusCommand' DataItem='Navigator.SearchViewModel' (HashCode=35535544); target element is 'GalaSoft.MvvmLight.Command.EventToCommand' (Name='null'); target property is 'Command' (type 'System.Windows.Data.Binding').. System.InvalidOperationException: Can't convert type GalaSoft.MvvmLight.Command.RelayCommand`1[System.Windows.RoutedEventArgs] to type System.Windows.Data.Binding.
   at MS.Internal.Data.DefaultValueConverter.Create(Type sourceType, Type targetType, Boolean targetToSource)
   at MS.Internal.Data.DynamicValueConverter.EnsureConverter(Type sourceType, Type targetType)
   at MS.Internal.Data.DynamicValueConverter.Convert(Object value, Type targetType, Object parameter, CultureInfo culture)
   at System.Windows.Data.BindingExpression.ConvertToTarget(Object value). 

It seems the Data Binding expression is having trouble being automatically converted to the type of the EventToCommand's Command property:

public System.Windows.Data.Binding Command { set; get; }

My initial inclination is to write an ValueConverter, but am I missing something obvious here?  Is there are way to have this conversion occur implicitly, or is this a bug in EventToCommand?

Thanks,

Sam

 

 

 

 

 

public RelayCommand<string> HandleFocusCommand
{
get;
private set;
}

 

 

Coordinator
Feb 28, 2010 at 10:41 AM

Hi,

Yes one small thing: You are using a RelayComand<string> however you did not set the CommandParameter to a string. If there is no CommandParameter, you should use a RelayCommand instead of RelayCommand<T>.

The only exception to that rule is when PassEventArgsToCommand is set to true on the EventToCommand object. In that case, and only in that case, you should use a RelayCommand<EventArgs> (or a type derived from EventArgs) so that the EventArgs parameter of the event is passed to the command in the ViewModel.

Please try this, and if it still fails, write again.

Thanks!

Laurent

Coordinator
Feb 28, 2010 at 10:44 AM
Edited Feb 28, 2010 at 10:44 AM

Something else: If you use Silverlight 4, please make sure that you are using the GalaSoft.MvvmLight.dll, GalaSoft.MvvmLight.Extras.dll and System.Windows.Interactivity.dll that are located in C:\Program Files\Laurent Bugnion (GalaSoft)\Mvvm Light Toolkit\VS10\Binaries\Silverlight

The VS10 directory contains the DLLs for Silverlight 4, the other ones are for Silverlight 3. The way to bind to a Command changed in Silverlight 4 due to the fact that we can now data bind to a DependencyObject (before it was only to FrameworkElement). This allowed me to delete quite a lot of code.

Thanks,

Laurent

Mar 1, 2010 at 3:57 PM

The RelayCommand<string> was just a leftover typo after trying to see if other variants would work.  I was actually using RelayCommand<RoutedEventArgs> before.

I believe the problem was that I was using either the Silverlight 3 dlls, or an older version of SL4 alphas.  I dowloaded the sample sln in your dragdrop blog post, and noticed that RelayCommand's command property was of type ICommand, whereas the version I was using was of type System.Windows.Data.Binding.  As soon as I switched, everything started working properly.

Thanks!

--Sam

Coordinator
Apr 18, 2010 at 7:59 PM

Hi,

As you are a previous user of the discussion tab on the MVVM Light Codeplex site, I would like to inform you that I decided to encourage the usage of StackOverflow for questions regarding the MVVM Light toolkit. Please tag your questions with the mvvm-light tag.

StackOverflow is an awesome site where tons of developers help others with their technical question.

http://stackoverflow.com/questions/tagged/mvvm-light

I will monitor this tag on the StackOverflow website and do my best to answer questions. The advantage of StackOverflow over the Codeplex discussion is the sheer number of qualified developers able to help you with your questions, the visibility of the question itself, and the whole StackOverflow infrastructure (reputation, up- or down-vote, comments, etc)

Thanks!

Laurent