3
votes

I have a context menu attached to a button on a toolbar on one of my controls in WPF (.NET 4.0). The context menu has a style assigned to it in the XAML that defines the context menu. Left clicking on the button opens the button's context menu if it isn't opened already.

Here's the relevant XAML:

<Button x:Name="fileButton" Foreground="White" Margin="7, 0, -3, 0" VerticalAlignment="Stretch" MaxHeight="70" MaxWidth="78" MinHeight="55" MinWidth="62" Style="{DynamicResource ImageButton}" utils:WpfImageUtil.Image="{StaticResource fileButton}" Template="{DynamicResource GlassButton}" Content="File" Visibility="Visible" Click="fileButton_Click">
    <Button.ContextMenu>
        <ContextMenu Style="{DynamicResource ContextMenuStyle}">
            <MenuItem x:Name="saveMenuItem" Header="Save" Click="saveMenuItem_Click" Style="{DynamicResource MenuItemStyle}" />
            <MenuItem x:Name="saveDrawingMenuItem" Header="Save Drawing" Click="saveMenuItem_Click" Style="{DynamicResource MenuItemStyle}" />
            <MenuItem x:Name="openMenuItem" Header="Open" Style="{DynamicResource MenuItemStyle}">
                <MenuItem x:Name="openFromFile" Header="From File" Style="{DynamicResource MenuItemStyle}" />
                <MenuItem x:Name="openFromDesktop" Header="From Desktop" Style="{DynamicResource MenuItemStyle}" />
            </MenuItem>
            <MenuItem x:Name="iconsMenuItem" Header="Icons" ItemsSource="{Binding}" Style="{DynamicResource MenuItemStyle}"/>
            <MenuItem x:Name="prefsMenuItem" Header="Preferences" Style="{DynamicResource MenuItemStyle}"/>
        </ContextMenu>
    </Button.ContextMenu>
</Button>

ContextMenuStyle is defined in a resource dictionary that is properly referenced.

When the context menu is opened with a left click, the style I have defined isn't applied to the menu, as shown below:

The style isn't applied on left click.

However, if the user right-clicks and opens the context menu the traditional way, the style is applied as expected:

The style is correctly applied on the first right click.

Afterwards, left-clicking the button will show the style correctly:

After the right click, left click is styled properly.

I have been trying to figure this out for some time, but haven't been able to come up with any reason that this issue occurs. It seems like some kind of bug to me, but I'm not entirely sure. I also don't know what happens at the lower level when controls are right-clicked on that would cause the style to get applied correctly.

1

1 Answers

4
votes

You should assign ContextMenu Style property in code (FindResource method msdn):

private void fileButton_Click(object sender, RoutedEventArgs e)
{
    if (fileButton.ContextMenu.Style == null)
        fileButton.ContextMenu.Style = this.FindResource("ContextMenuStyle") as Style;
    fileButton.ContextMenu.IsOpen = true;
}

ContextMenu Overview (http://msdn.microsoft.com/en-US/library/ms742558.aspx)

A ContextMenu is attached to a specific control. The ContextMenu element enables you to present users with a list of items that specify commands or options that are associated with a particular control, for example, a Button. Users right-click the control to make the menu appear. ...

When you right-click on the control, style will be applied to the ContextMenu. So if you want to open ContextMenu in code, you should check if style is equal null and if it's true, you should assign appropriate style.