Question

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?

Was it helpful?

Solution 3

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.

OTHER TIPS

First of all: ContextMenu is completly outside of your Controls VisualTree so normal Binding won't work at all unless you change your Binding as descibed here

Since you are using a code Behind Event try the following:

<MenuItem Header="{Binding MenuText}" MouseDown="MenuItem_MouseDown" />

And

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

You should you Command Binding, ensuring you bind to the same context where the command is created.

If <MenuItem Header="{Binding MenuText}" ...> is binding correctly, than you should create a RelayCommand in the same context. and then bind to the click via

<MenuItem Click="{Binding ClickCommand}" ...?

But if you need a quickfix, you could use PreviewMouseButtonDown/Up in your NavBarControl and then check what was click in the handler?

See: get the name of the element on which I clicked / mousedown

<dxn:NavBarControl PreviewMouseUp="NavBar_PreviewMouseUp" ...>

And in the code behind.

private void NavBar_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
    var mouseWasUpOn = e.Source as MenuItem;
    if (mouseWasUpOn != null)
    {
        string header = mouseWasDownOn.Header;
    }
}

But do note, this is just a workaround if the normal event handlers are not working. Ideally you should be binding to a Command!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top