14

I have a custom Popup that overlays part of my screen. When it is open, I want to disable tabbing into the UserControl behind it. I do not want to use the IsEnabled property because I do not want to gray out all the controls.

Is there another property that does the same thing? IsTabStop only prevents the tab from stopping on the UserControl itself, not it's children, and IsFocusable isn't a valid property for a UserControl.

Rachel
  • 130,264
  • 66
  • 304
  • 490
  • 1
    `Focusable` is a property on `UserControl` inherited from `UIElement`. Setting `Focusable="False"` and `IsHitTestVisible="False"` should solve your problem – Fredrik Hedblad Oct 12 '11 at 19:37
  • @Meleak That does not work. I can still tab to the controls inside my UserControl – Rachel Oct 12 '11 at 19:53
  • 2
    Does `KeyboardNavigation.TabNavigation="None"` on your UserControl help? Have a look [here](http://msdn.microsoft.com/en-us/library/system.windows.input.keyboardnavigation.tabnavigation.aspx) – LPL Oct 12 '11 at 21:19
  • @LPL I take it back, that does work :) I was setting my `KeyBoardNavigation.TabNavigation` inside my UserControl, so the trigger was having no effect. If you post it as an answer I'll accept it. – Rachel Oct 13 '11 at 12:45
  • `IsTabStop` seems to not work on user controls that have `ItemsSource` assigned. See [this](https://stackoverflow.com/questions/5078937/istabstop-false-has-no-effect-on-my-wpf-app/8311213#comment42991524_5078937) – z33k Aug 13 '21 at 14:32

6 Answers6

23

Use the KeyboardNavigation.TabNavigation Attached Property with KeyboardNavigationMode.None on your container control.

KeyboardNavigation.TabNavigation="None"
LPL
  • 16,827
  • 6
  • 51
  • 95
5

You can bind IsTabStop on the child controls to IsTabStop on the UserControl.

That way, you only have to set it once.

Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154
  • 1
    I currently have 35 controls in my UserControl... I don't really want to create a binding on all of them, and have to remember to set that binding for new controls. Isn't there an easier way of disabling it on the UserControl level? – Rachel Oct 12 '11 at 19:50
  • A loop? foreach (Control control in grid.Children) { control.IsTabStop = false; } – Carlo Oct 12 '11 at 22:41
  • It'd have to be a loop that goes through all levels of child objects.... I suppose it's possible – Rachel Oct 13 '11 at 01:53
0

Just bind that property to the user control.

    <UserControl x:Class="PDV.UserControls.InputField"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 // etc... then:                     
                 x:Name="Root" KeyboardNavigation.TabNavigation="Local" >

        <Grid>
           <TextBox Name="textBox"  
                TabIndex="{Binding Path=TabIndex, ElementName=Root}" 
                IsTabStop="{Binding Path=IsTabStop, ElementName=Root}"  />                
        </Grid>
    </UserControl>
Tono Nam
  • 34,064
  • 78
  • 298
  • 470
0

The solution with KeyboardNavigation.TabNavigation="None" seems to bubble up to all other parent containers in my case. This is maybe not wanted in some scenarios. So I came up with a PreviewKeyDown-Event in code behind like this:

    private void OnPreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Tab)
        {
            ((Control)sender).Focus(); 
            e.Handled = true;
        }
    }
wulf11
  • 79
  • 8
0

This worked for me:

// ...

var applicationWindow = Application.Current?.MainWindow;
if (applicationWindow != null)
{
    KeyboardNavigation.SetIsTabStop(
        element: applicationWindow, 
        isTabStop: false);

    KeyboardNavigation.SetTabNavigation(
        element: applicationWindow, 
        mode: false);
}

// ...

Note: it is good to have a backup plan for restoring the Keyboard navigation in case if the above is needed only temporarily.

Arsen Khachaturyan
  • 7,904
  • 4
  • 42
  • 42
0

You could write an attached property that you would set in the top element.
That attached property would recursively set IsTabStop to false in all the child elements.

Let me know if you need any help getting this to work.

Louis Kottmann
  • 16,268
  • 4
  • 64
  • 88