Search code examples
c#wpfxamluser-interfacetextblock

How to Focus on a TextBlock in XAML WPF with keyboard (for example with tab) in a Grid view?


I have a textblock which has a binding path with a converter, but I am unable to operate that with tab button. Textblock has an Onclick property which opens another page. I want to open that screen with keyboard. Kindly help!

I tried Keyboard Focusable in and also Focusable=On but its not working


Solution

  • You could (should!) use a Button and make it look like a TextBlock:

    <Button Click="Button_Click">
        <Button.Template>
            <ControlTemplate TargetType="Button">
                <TextBlock Text="Focus me!" />
            </ControlTemplate>
        </Button.Template>
    </Button>
    

    The above element can be both keyboard focused and clicked and still looks like a TextBlock.

    Remember that WPF controls are lookless which means that a control's behavior is independent from its appearance.