Alex G
Alex G

Reputation: 595

WPF Menu Item doesn't get closed

I want to re-style my ContextMenu with following style

   <ControlTemplate x:Key="{x:Static MenuItem.SubmenuItemTemplateKey}" TargetType="MenuItem">
    <Border x:Name="templateRoot" BorderBrush="{TemplateBinding BorderBrush}"
            BorderThickness="{TemplateBinding BorderThickness}" 
            Background="{TemplateBinding Background}" Height="22" SnapsToDevicePixels="true">
        <Grid>
           <Button>
                <ContentPresenter 
                        Name="HeaderHost"
                        ContentSource="Header"
                        RecognizesAccessKey="True"/>
            </Button>
        </Grid>
    </Border>
    <ControlTemplate.Triggers>
        <Trigger Property="IsHighlighted" Value="True">
            <Setter Property="Background" TargetName="templateRoot" Value="Transparent"/>
            <Setter Property="BorderBrush" TargetName="templateRoot" Value="{StaticResource MenuItem.Highlight.Border}"/>
        </Trigger>
        <Trigger Property="IsEnabled" Value="false">
            <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

Once I click on my context menu item it triggers my call back but the menu itself stays on the screen. How can I trigger closing logic?

Thanks

Upvotes: 2

Views: 138

Answers (1)

Glen Thomas
Glen Thomas

Reputation: 10744

The Button is handling the click event and stopping it from bubbling up the visual tree to the MenuItem.

Setting IsHitTestVisible on the Button would stop the Button handling the click event, but would also stop MouseOver/Pressed styling.

<Button IsHitTestVisible="False">
    <ContentPresenter 
    Name="HeaderHost"
    ContentSource="Header"
    RecognizesAccessKey="True"/>
</Button>

Upvotes: 2

Related Questions