1

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?

3
  • 2
    where do you register the MouseDown-Event? I only the the Click-Event of MenuItem Commented Apr 7, 2014 at 9:27
  • 4
    What Version of DevExpress are you using? Commented Apr 7, 2014 at 9:29
  • @Jehof 13.2 right now. Commented Apr 9, 2014 at 6:42

3 Answers 3

2
+150

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; } 
Sign up to request clarification or add additional context in comments.

Comments

1

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!

2 Comments

Yeah, I know, thanks, but the boss wanted to see if the menu click correctly opened a Delphi window in the proper area of the view. Once I got the Click event working, the other stuff was quite easy.
I have tried using a command as you say, but instead a DelegateCommand and I get an InvalidCastException: "Unable to cast object of type 'System.Reflection.RuntimeEventInfo' to type 'System.Reflection.MethodInfo'." I can't find a RelayCommand class.
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.