Reputation: 5550
Now imagine I have ten controls all bound inside a stackpanel
.
By default, when TAB
is pressed, the focus will move from control 1 subsequently to control 10.
Now what I want is, after focus moving from control 1
to control 2
, when the user press TAB
again, the focus will go back to control 1
. So far I can only mess around with the sequence by using KeyboardNavigation.TabIndex="N" where N = "0,1,2,3,.."
, but what I ultimately want is skipping the remaining 8 controls.
Please do not suggest TabNavigation="NONE" or IsTabStop="False" to skip the control, I don't want to mess with other controls and yea, I'm fine with hardcode sequence.
Upvotes: 1
Views: 232
Reputation: 1955
Override the preview key down event on the controls that you want to have control over and if its tab then do what you want.
Here is an example if it was something like a textbox you could use something like this. Atach the event handlers either in c# or in the xaml.
btn1.PreviewKeyDown += new KeyEventHandler(btn1_KeyDown);
btn2.PreviewKeyDown += new KeyEventHandler(btn2_KeyDown);
then
private void btn1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Tab && (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift)))
{
//do what you want when shift+tab is pressed.
e.Handled = true;
}
else
{
btn2.Focus();
e.Handled = true;
}
}
private void btn2_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Tab && (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift)))
{
//do what you want when shift+tab is pressed.
e.Handled = true;
}
else
{
btn1.Focus();
e.Handled = true;
}
}
Upvotes: 1