I have a user control containing a few CheckBoxes. Each CheckBox has a unique access key. Now if I use more than 1 instance of this user control in a dialog, then the access keys doesn't work properly. The focus goes correctly to the checkboxes when access key is pressed but the checkbox doesn't get checked or unchecked.
To illustrate this, lets say you have this XAML:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<CheckBox Grid.Row="0" Content="_First" Margin="5"/>
<CheckBox Grid.Row="1" Content="F_urst" Margin="5"/>
</Grid>
</Window>
If you run the above code, the checkboxes will get checked/unchecked on Alt+F or Alt+U.
Now lets say instead you have this XAML:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<CheckBox Grid.Row="0" Content="_First" Margin="5"/>
<CheckBox Grid.Row="1" Content="_Furst" Margin="5"/>
</Grid>
</Window>
Here both checkboxes have same access key which is 'F'. Now if we do Alt+F, the focus will iterate over the checkboxes without selecting them. I want the checkboxes to get checked or unchecked as usual.
Does anyone know how to solve this problem? Or even a better idea to handle such scenarios?
PS: I know it doesn't make sense to have duplicate access keys in the same dialog, but the above code is only for illustration purpose. My problem (as I explained at the top) is that I am using a user control multiple times in a dialog and hence access keys within the control are repeated.
This behaviour is normal. How would you propose any different scenario would work? If two controls have the same access key, then the key that would normally select the control now alternates between the two. As with all Checkbox
controls, you can simply press the Spacebar to select or unselect it. So the solution is to select the control with the access key and check or uncheck it using the Spacebar.
UPDATE >>>
Personally, I wouldn't attempt to get around this behaviour, instead preferring to explain to the users how the system actually works. If it really bothers you, a workaround could be found, but it would be a lot of work and in my opinion, not worth it.
For example, you could add code to your Dialog
constructor that would dynamically set all the properties that define access keys. The problem with this however, is that outside of the Dialog
, you'd need to keep a record of how many other Dialog
controls were currently open and which letters were used for which properties for each of them.
Good luck if you do choose to wander this lonely path.