0

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?

Rand Random
  • 7,300
  • 10
  • 40
  • 88

1 Answers1

1

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>
Michal Ciechan
  • 13,492
  • 11
  • 76
  • 118
  • I have a similar issue and am not understanding why if a menu item in the window is being focused on the window would not appear in the foreground? Also, the issue seems to be random in that sometimes the app/window will appear in the foreground and other times it will appear behind other windows... – m4gik Jan 26 '18 at 19:50
  • @m4gik ask a question and point to it, I will try to take a look – Michal Ciechan Jan 27 '18 at 17:16