Tatarinho
Tatarinho

Reputation: 784

Remove focus from RibbonMenuItem in WPF

I have a simple Ribbon in my WPF application with RibbonGroup and RibbonMenuButton in group. I want to remove focus after clicking this button. Now I have to double click or click on something else to lose focus.

<Custom:Ribbon Name="RibbonMenu" Focusable="True" Background="WhiteSmoke" HorizontalAlignment="Stretch" VerticalAlignment="Top" Loaded="RibbonLoaded" Grid.Row="0" Grid.ColumnSpan="2">
        <Custom:RibbonTab Name="home" Header="Home" KeyTip="H" Focusable="True" >
            <Custom:RibbonGroup Focusable="True" x:Name="utilsGroup" Header="Utils">
                <Custom:RibbonMenuButton x:Name="btUtils1" PreviewMouseLeftButtonDown="btUtils1_MouseLeftButtonDown" Label="Utility no 1" LargeImageSource="Resources/utils.png"/>
            </Custom:RibbonGroup>
        </Custom:RibbonTab>
</Custom:Ribbon>

And my codebehind:

private void btUtils1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e){
       home.Focus();
}

It's not working at all. I looked for solution but it seems that anything from net can help me. Any suggestion?

Upvotes: 1

Views: 410

Answers (1)

Peter Duniho
Peter Duniho

Reputation: 70701

Without a good Minimal, Complete, and Verifiable example and especially a more precise explanation of what it is you actually want the code to do — i.e. state clearly and exactly what the code does now, and what you want it to do instead — it is hard to know for sure what you are trying to accomplish.

That said, in the example you gave, you've got a RibbonMenuButton that, when clicked, opens a dropdown that remains open until the mouse capture is lost (i.e. the user uses the mouse or keyboard to direct attention elsewhere).

If you are trying specifically to cause this dropdown to disappear when clicked, you could accomplish this by setting the IsDropDownOpen property to false. Note that you'd have to do this after all the input handling is done, since it's the input itself that causes the dropdown to open. You can use Dispatcher.InvokeAsync() for that purpose. E.g.:

private void btUtils1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    Dispatcher.InvokeAsync(() => btUtils1.IsDropDownOpen = false);
}


That said, are you sure you really want a RibbonMenuButton here? If you don't want the control to remain open or otherwise visibly affected by clicking, maybe what you really want is a RibbonButton. That control type acts like a normal button, and won't have any visible after-effect if clicked.

Upvotes: 1

Related Questions