I have the following Window defined:
<Window x:Class="ShortCutInTabControlproblem.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">
<DockPanel>
<TabControl>
<TabItem Header="_1st Tab">
<DataGrid ItemsSource="{Binding Persons}" />
</TabItem>
<TabItem Header="_2nd Tab">
</TabItem>
</TabControl>
</DockPanel>
With the following code behind:
public partial class MainWindow : Window
{
public ObservableCollection<Person> Persons { get; set; }
public MainWindow()
{
InitializeComponent();
Persons = new ObservableCollection<Person>
{new Person() {Name = "John", Age = 39}, new Person() {Name = "Doe", Age = 34}};
DataContext = this;
}
}
And finally the following "Model":
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
The strangest thing happens when I try to add a row to the datagrid on the first tab ("1st Tab") and the first key I press is '2'. This will activate the second tab ("2nd tab"), as this has a shortcut defined.
If I edit an existing grid, e.g. the age of John and press '2', then nothing happens (except the age is changed to 2).
Is this a known bug and are there any known fixes? Do You know of a way to require Alt+'2' to be pressed, and not just '2'?
Please help me...
Thanks in advance Søren Rokkedal
It's actually a know issue that is supposed to be fixed in a future release of .NET.
In the meantime you can use a custom TabItem to fix your problem :
public class MyTabItem : TabItem
{
protected override void OnAccessKey(AccessKeyEventArgs e)
{
if ((Keyboard.Modifiers & ModifierKeys.Alt) != ModifierKeys.Alt)
{
return;
}
base.OnAccessKey(e);
}
}
You can also write the following fix to solve the issue for all UIElements :
EventManager.RegisterClassHandler(typeof(UIElement), AccessKeyManager.AccessKeyPressedEvent, new AccessKeyPressedEventHandler(OnAccessKeyPressed));
where OnAccessKeyPressed would implement the same logic.