Rand Random
Rand Random

Reputation: 7440

Open Window Activate not working with button inside menu

I have a really strange behaviour, and I hope someone can help me out.

I have the following XAML layout:

<StackPanel Orientation="Horizontal">
    <Menu>
        <Menu.Items>
            <MenuItem Padding="2,0,2,0">
                <MenuItem.Header>
                    <Button Content="Details"
                            Click="Details_Click" />
                </MenuItem.Header>
            </MenuItem>
        </Menu.Items>
    </Menu>
    <Button Content="Details"
            Click="Details_Click" />
</StackPanel>

Please notice that both buttons have the same Event registered. The Details_Click Event looks like this:

private void Details_Click(object sender, RoutedEventArgs e)
{
    var viewer = new DictionaryViewer();
    viewer.ShowActivated = true;
    viewer.Show();
    viewer.Topmost = true;
    viewer.Topmost = false;
    viewer.Activate();
    viewer.Focus();
    e.Handled = true;
    return;
}

Now I am facing the problem, even with all the code from above the Window doesnt show up activated when I press the button inside the Menu but outside of it works with just .Activate();. (How I know that window isnt activated: need 2 clicks to close/minimize/maximize it)

Why would my XAML Layout ruin the Activation of the DictionaryViewer(); window, with the button inside Menu?

(To your information the DictionaryViewer is totally empty, its a fresh window nothing implemented yet)

Edit:

Yes, I know there is the MenuItem_Click Event that may make it work, but I need/want the button inside the Menu how can I fix this issue?

Upvotes: 0

Views: 165

Answers (1)

Michal Ciechan
Michal Ciechan

Reputation: 13888

THe reason this is happening is because the Button inside the MenuItem is gaining Focus after the Window has opened.

If you set the Focusable property of the button inside MenuItem, this fixes the issue.

E.g.

<StackPanel Orientation="Horizontal">
    <Menu>
        <Menu.Items>
            <MenuItem Padding="2,0,2,0">
                <MenuItem.Header>
                    <Button Content="Details"
                            Click="Details_Click"
                            Focusable="False" />
                </MenuItem.Header>
            </MenuItem>
        </Menu.Items>
    </Menu>
    <Button Content="Details"
            Click="Details_Click" />
</StackPanel>

Upvotes: 1

Related Questions