Evil Beaver
Evil Beaver

Reputation: 378

Disable whole user control focusing from keyboard

I have UserControl in a window. When user walks window with "Tab" key user control gets focused and dashed border drawn around it. How to prevent this behavior? enter image description here

Upvotes: 5

Views: 9388

Answers (3)

Evil Beaver
Evil Beaver

Reputation: 378

It was my mistake. I had xaml:

<ContentControl>
  <ScrollViewer name="viewport"/>
</ContentControl>

and "viewport.Content" was set to my UserControl from code-behind.

It was a ContentControl who draw the focus border. I removed it and left only a . Problem solved.

Upvotes: 0

Anatoliy Nikolaev
Anatoliy Nikolaev

Reputation: 22702

Try it for an control set Focusable = "False". Example:

<Grid Focusable="False">
...
</Grid>

Or set the Style to focus yourself:

<Grid FocusVisualStyle="{x:Null}" />

Also, the Style of focus might be:

<Style x:Key="MyItemFocusVisual" TargetType="{x:Type Control}">
    <Setter Property="Control.Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Control}">
                <Border SnapsToDevicePixels="True" CornerRadius="0" BorderThickness="5" BorderBrush="#7B2F81" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Using:

<Grid Focusable="True" FocusVisualStyle="{StaticResource MyItemFocusVisual}" ... />

Output

enter image description here

Upvotes: 8

Chris W.
Chris W.

Reputation: 23280

If you just want to keep it from accepting focus via Tabbing just declare it on the object via IsTabStop="False" or you can edit the control Template for it and get rid of the Focus changes.

Upvotes: 5

Related Questions