Skip to content

How to set keyboard focus to added inputs? #231

Answered by h0lg
h0lg asked this question in Q&A
Discussion options

You must be logged in to vote

@edgarfgp Thank you for pointing me in the right direction :)

I've modified your suggestion slightly to only set the focus if the boolean passed is true, clean up the handler only after it has run and open it up to be used with InputElements other than TextBox:

open System.Runtime.CompilerServices
open Avalonia.Input
open Fabulous
open Fabulous.Avalonia

module FocusAttributes =
    /// Allows setting the Focus on an Avalonia.Input.InputElement
    let Focus =
        Attributes.defineBool "Focus" (fun oldValueOpt newValueOpt node ->
            let target = node.Target :?> InputElement

            let rec focusAndCleanUp x y =
                target.Focus() |> ignore
                tar…

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by edgarfgp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants