Expression Blend Samples InvokeDataCommand bug

.NET, Blend, MVVM, Silverlight, Technical stuff, Work, WPF
See comments

My good friend and super hero programmer Pete Blois posted an impressive collection of samples using and extending Expression Blend behaviors and triggers on Codeplex some time ago. There are a few real treasures in there, and some of them that I would like to integrate in my MVVM Light toolkit, thus avoiding to reinvent the wheel. I am still checking the license (the Samples use the MS-PL license) but I think it should work just fine.

One feature in particular that I find super helpful is InvokeDataCommand. This Trigger allows any event to be bound to a command, for example an ICommand or a RelayCommand. In the moment, if you use the MVVM Light toolkit, you need a few lines in the code behind to handle the event and relay to the corresponding RelayCommand on the ViewModel. No big deal, but InvokeDataCommand is in XAML only, and this can even be done in Blend, so it is even easier for visual people (designers, integrators…). With InvokeDataCommand, the code looks like this:

<Button Content="Click me">
        <si:EventTrigger EventName="Click">
           <i:InvokeDataCommand Command="{Binding ClickCommand}" />
        <si:EventTrigger EventName="MouseEnter">
            <i:InvokeDataCommand Command="{Binding MouseOverCommand}" />

However, as described here, there seems to be a bug in InvokeDataCommand which prevents it to work in WPF. The code above works fine in Silverlight, but fails in WPF. I downloaded the projects and took a look at the code, and thanfully the bug was not too hard to spot. In Silverlight, to work around the limitation that bindings can only be applied on FrameworkElements, a BindingListener class has been used. However, this class is not needed in WPF, and in fact it causes the bug (note that in the contrary of what is described in the bug report, I do not witness any exception, the code des’t crash but it will just fail).

I submitted a corrected version of InvokeDataCommand.cs on the Codeplex site, and hope it will be reviewed and integrated soon. In the mean time, feel free to contact me if you need details about the fix.

The issue in Codeplex can be seen here.

Previous entry | Next blog entry

Comments for Expression Blend Samples InvokeDataCommand bug