Problem with DelegateCommand

Topics: Prism v4 - Silverlight 4
Jun 12, 2012 at 9:22 AM


in my ViewModel i add a delegatecommand to a menuitem

But the method is never called



 Private Sub initMenuCommands(ByVal pSubMenuItems As ObservableCollection(Of SubMenuItem))
   For Each itm In pSubMenuItems
     itm.MenuSelected = New DelegateCommand(Of SubMenuItem)(AddressOf MenuSelected)
 End Sub

Private Sub MenuSelected(pSubMenuItem As SubMenuItem)
   ' Do anything     
End Sub

In my View i have the following xaml-code:
<sdk:HierarchicalDataTemplate x:Key="MainMenuTemplate" ItemsSource="{Binding ChildMenuItems}">
  <Button Command="{Binding MenuSelected}" CommandParameter="{Binding}" >
        <StackPanel Orientation="Horizontal">
          <Image Margin="0,0,5,0" Source="{Binding ImageId,Converter={StaticResource ImageConverter}}"/>
          <TextBlock Text="{Binding Description}" FontStyle="Italic" Foreground="#777" />

Can anyone help me

Jun 12, 2012 at 9:05 PM

Hi Jens,

I tried reproducing the scenario you are mentioning using a TreeView control, and I could execute the DelegateCommand without problems. You can find my sample application in my SkyDrive account with the name DelegateCommand problem:

As a starting point, you could check my aforementioned implementation and find any difference between it and your application that might be causing this problem.

Based on my understanding, this kind of problem could appear if:

  • The MenuSelected property of each SubMenuItem is set after the view model is assigned as the DataContext of the view. (Remember that after setting the view's view model, any change in the view model needs to be notified to the view.)
  • The HierarchicalDataTemplate is not being applied to the corresponding SubMenuItem.

I hope you find this useful,

Damian Cherubini

Jun 13, 2012 at 9:52 AM


Thank you for the help

I know now my problem but the solution to this missing

My MenuControl is in a seperate Assembly. The Command doesn't invoke.

When i put my Menu in the MenuModul it works very well.

I have 2 Views in the Solution:

MenuView where the control is in the control library

MenuViewDirect where the control is in the modul

Here is my Solution:

Do you have any Idea to solve my problem



Very Thanks


Jun 13, 2012 at 7:58 PM

Hi Jens,

I have downloaded you repro-sample application, but I couldn't reproduce the issue you are mentioning.

I tried running the application with the MenuView (which uses the MenuControl defined in the Ar.Base.Controls project) and the MenuViewDirect (which contains its own menu.) In both cases, the commands of each menu item where invoked correctly. We have checked this by setting a breakpoint, in the MenuSelected method of the MenuViewModel class.

Based on this, I believe the problem you are describing may be related to the your environment rather that the code. As a possible approach you could try cleaning you solution (by pressing Clean Solution in the Build menu), deleting the obj and Bin folders of each project and deleting the files contained in the ClientBin folder of the Ag.Web project, so that the next time you run the application, it will be built again.


Damian Cherubini