Trevor
Trevor

Reputation: 373

Is there a way to disable focusability on an element and all its children?

I am building a UWP app and am wondering if there's a simple and performant way to disable focusability on an element and all its children. IsTabStop=false only disables focusability on an individual element, but doesn't get inherited by its children.

Upvotes: 1

Views: 758

Answers (3)

Trevor
Trevor

Reputation: 373

Reading through the focus docs for Control and it states:

In order to be focusable, each of these must be true:

  • Visibility is Visible
  • IsEnabled is true
  • IsTabStop is true
  • The control must be instantiated with its template loaded

IsEnabled disables focus for an entire control (children included). So one approach would be to surround everything in a <ContentControl> element (assuming it doesn't already inherit from Control) and just set IsEnabled="False".

Upvotes: 1

crunchytortoise
crunchytortoise

Reputation: 91

Since UWP does not have a focusable property the best thing to do would be to create a GotFocus handler which immediately returns focus to the element that previously had it.

Upvotes: 1

BlueTriangles
BlueTriangles

Reputation: 1224

You could:

  1. Loop through the elements and apply IsTabStop = false on them (just get the children from the main element and loop on them)

  2. Do some binding, binding each control's IsTabStop value to the IsTabStop value of the main element. Though, depending on how many controls you have, this could get tedious...

Upvotes: 1

Related Questions