2

Because ToolbarTray does not support proper ItemsSource binding I'm doing things in code behind. Namely filling the tray. How can I tell which toolbar the contextmenu was opened for (with CommandParameter)?

<UserControl.Resources>
    <ContextMenu x:Key="ToolbarContextMenu">
        <MenuItem Header="Move to top" Command="{ui:CommandHandler MoveToTop}" />
        <MenuItem Header="Move to left" Command="{ui:CommandHandler MoveToLeft}" />
        <MenuItem Header="Make float" Command="{ui:CommandHandler MakeFloat}" />
    </ContextMenu>
</UserControl.Resources>
<ToolBarTray x:Name="MainToolbarTray" Orientation="{Binding Orientation}" ContextMenu="{StaticResource ToolbarContextMenu}"/>

In example above I use a toolkit where commands in ViewModel are decorated with attribute and in xaml command can be defined like this.

(Not the same as suggested. I don't want to find ToolbarTray, but the Toolbar)

EDIT: I just assigned the ContextMenu to Toolbar in code behind and used PlacementTarget as suggested.

char m
  • 7,840
  • 14
  • 68
  • 117
  • 1
    Possible duplicate of [Get owner of context menu in code](http://stackoverflow.com/questions/1884117/get-owner-of-context-menu-in-code). Sorry, I didn't find better one, but idea with `menuItem.Parent.PlacementTarget` is the answer. – Sinatr Dec 18 '15 at 11:25
  • @Sinatr : Not the same. I don't want to find ToolbarTray, but the Toolbar – char m Dec 18 '15 at 11:42
  • Maybe this can't be done. Maybe I just have to assign contextmenu for Toolbar in code behind. – char m Dec 18 '15 at 11:45

0 Answers0