Search code examples
c#wpfmvvmdevexpressprism-4

Why is event not firing in WPF/MVVM application?


I have the following NavBar, with the Content data template marked up as follows:

<dxn:NavBarControl Name="SideMenuNavBar" DataContext="{Binding}" IsEnabled="{Binding Enabled}" ItemsSource="{Binding Bars}" HorizontalAlignment="Left"  VerticalAlignment="Stretch" Margin="1">
  <dxn:NavBarControl.Resources>
    <Style TargetType="dxn:NavBarGroup">
      <Setter Property="Header" Value="{Binding DisplayText}"/>
      <Setter Property="Content" Value="{Binding MenuItems}"/>
      <Setter Property="DisplaySource" Value="Content"/>
      <Setter Property="ContentTemplate">
        <Setter.Value>
          <DataTemplate>
            <TreeView ItemsSource="{Binding}">
              <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Path=ChildItems}" DataType="{x:Type common:MenuItemBase}">
                  <TextBlock Text="{Binding ItemText}" HorizontalAlignment="Stretch">
                    <TextBlock.ContextMenu>
                      <ContextMenu DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}">
                        <MenuItem Header="{Binding MenuText}" Click="MenuItem_OnClick" />
                      </ContextMenu>
                    </TextBlock.ContextMenu>
                  </TextBlock>
                </HierarchicalDataTemplate>
              </TreeView.ItemTemplate>
            </TreeView>
          </DataTemplate>
        </Setter.Value>
      </Setter>
    </Style>
  </dxn:NavBarControl.Resources>
  <dxn:NavBarControl.View>
    <dxn:NavigationPaneView GroupDisplayMode="Text" ItemDisplayMode="Text" MaxVisibleGroupCount="12"/>
  </dxn:NavBarControl.View>
</dxn:NavBarControl>

The binding is working, as my one treeview of menu items appears correctly, yet when I click (MouseDown event) nothing happend, or when I double click, for the following handler, the handler does not execute:

private void Control_OnMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
  var x = sender;
}

My breakpoint on var x = sender; never gets hit.

NOTE: I know I should not be using events but rather commands, or some other much less coupled code, but I urgently need to demo what happens when the user clicks a menu item, and before the event, my code for a command didn't fire either. What could be wrong here?


Solution

  • I'm quite new to WPF, and very new to the DevExpress WPF suite, so I just copied the original code above from an example somewhere. Then I thought to myself, why a ContextMenu? Then, I right clicked the menu item, and a blank context menu item appeared, and that fired the event. I removed the bloody context menu, and now my menu item is firing the event, like I wanted when I asked this question. The new structure of the TreeView is as follows:

    <TreeView.ItemTemplate>
      <HierarchicalDataTemplate ItemsSource="{Binding Path=ChildItems}" DataType="{x:Type menus:MenuItemBase}">
        <MenuItem DataContext="{Binding}" Header="{Binding ItemText}" Click="MenuItem_OnClick" />
      </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
    

    I'm still trying to get proper command binding working, but this answers my question about the events.