wpfvb.netmvvmlambdacommandbinding

Bug(?) in WPF Command Binding


I'm using WPF (.NET 4.5) and VB.NET with MVVM Light. A Button in my Toolbar binds to a RelayCommand of the underlying VM. The Button looks like this in XAML:

<Button Command="{Binding Path=BringLayerForwardCommand}" CommandParameter="{Binding SelectedLayer}">

Underlying command is declared like this in the ViewModel:

Public Property BringLayerForwardCommand As New RelayCommand(Of Layer) _ 
  (Sub(p) BringLayerForward_Executed(p), _
  Function(p) p IsNot Nothing AndAlso _
              Me.Layers IsNot Nothing AndAlso 
              (p.ZOrder < Me.Layers.Where(Function(l) l.IsFront = p.IsFront).Max(Function(l2) l2.ZOrder)))

The Button behaves like this at runtime:

  1. It never gets enabled.
  2. The breakpoint at CanExecute part (2nd parameter of RelayCommand constructor above) never gets hit.
  3. If I remove the lambda part from the anonymous function, the breakpoint starts getting hit and the Button gets enabled.

After removing the lambda part, the above command looks like this:

Public Property BringLayerForwardCommand As New RelayCommand(Of Layer) _
  (Sub(p) BringLayerForward_Executed(p), _
  Function(p) p IsNot Nothing AndAlso _
              Me.Layers IsNot Nothing)

Is this a bug or am I really missing something obvious?

EDIT

Further investigation tells that if I move the entire anonymous function to a regular function, it all starts working just fine. Really seems like a bug.


Solution

  • I thought you may want some closure on this issue ;).

    I suspect it was "Issue 7721: Closures not working with RelayCommand" see: http://www.mvvmlight.net/installing/changes.

    This bug has been fixed in 5.4.1.