Adding to my original comment
In fact, there is a slight difference in the behavior of Notepad and Word 2013. Hotkey control is located in the menu that removes Focus as soon as you press Alt, and you can see that they get focus, because the carriage of the text area disappears, and Esc then returns focus to the TextBox, clearing "_". Now in your code example, if you put two shortcuts or even one inside a, and then press Alt, you will get the same behavior as Word or Notepad. Without a menu, there is no control that wants to take intermediate focus.
This is simply a problem when the Menu gets deferred focus to handle these hot keys in its own FocusManager.IsFocusScope .
Another snippet from MSDN :
The following script illustrates the change in keyboard focus and logical focus in Windows Presentation Foundation (WPF), which has a window with a text box and a menu that has a MenuItem. When the keyboard focus changes from TextBox to MenuItem, the TextBox will lose the keyboard, but retain the logical focus for the focus area of โโthe Window. MenuItem gets keyboard focus and gets logical focus for the focus area of โโthe menu. When the keyboard focus returns to the root window, the item in the focus area of โโthe Logically Focused Window will receive the keyboard focus, which in this case is the TextBox. TextBox now has keyboard focus and logical focus. MenuItem loses keyboard focus, but retains the focus focus of the menu.
Decision:
If you simply cannot hold back the fact that you are limited by the grouping requirement imposed by Menu to achieve the hotkey functionality, you can do something like:
<Grid Margin="25"> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="15" /> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Menu Width="0" Height="0"> <Label x:Name="label1" Content="_Textbox 1" Target="{Binding ElementName=textbox1}" /> <Label x:Name="label2" Content="T_extbox 2" Target="{Binding ElementName=textbox2}" /> <Label x:Name="label3" Content="Another Loose Label to Link Text_Box 1" Target="{Binding ElementName=textbox1}" /> </Menu> <Label Grid.Row="0" Grid.Column="0" Content="{Binding ElementName=label1, Path=Content}" /> <TextBox x:Name="textbox1" Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="2" Margin="15 0 0 0" VerticalAlignment="Center" /> <Label Grid.Row="2" Grid.Column="0" Content="{Binding ElementName=label2, Path=Content}" /> <TextBox x:Name="textbox2" Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2" Margin="15 0 0 0" VerticalAlignment="Center" /> <Rectangle Grid.Row="3" Grid.RowSpan="2" Grid.Column="1" Margin="10" Fill="Tomato" /> <Label Grid.Row="4" Grid.Column="2" Content="{Binding ElementName=label3, Path=Content}" /> </Grid>
So, in this setting, we do not use any custom Style for any of the controls. To a large extent, we create a new top level Menu and add to it all the necessary Label keyboard shortcuts and set its width and height to 0.
Thus, in the actual visible layout of the Grid we can place the visible Label anywhere we select, and avoid duplicating the code when specifying Label.Content twice for each Label (once in the menu and then in the actual layout), we use binding. to get the contents for the visual tag from the corresponding tag in the menu.
Update
Reply to OP comment:
I'm not sure if this will be practical in my application. I have hotkeys throughout the application, some of which duplicate the firewall. This workaround could not be supported.
Well, I donโt see how duplicates or having material in everything matter for this tbh.
- If you have a repeating hotkey in multiple
Window , then define the Content for this duplicate element as a String resource in xaml or your .resx, you can refer to it wherever you need it. - Suitable - itโs good that you donโt have anything special for maintainability. Your layout is not affected, and you are not limited by anything. If you have one
<Menu> sits hidden at the top level of each of your Window , which defines the hot keys for this Window and inside this window is any control that wants this functionality. Binds to the corresponding child menu item. I think it combines the hotkey area in Window in one place than in several different places.
Finally, I am not trying to impose this approach on you, it comes down to your personal preferences. If this is not convenient for you, you can get Label subclasses and implement the deferred focus functionality on your own or raise a Microsoft error to find out if they can possibly contact it for you or find an alternative solution or let go of the hot - all functions.