C#, WPF Desktop, set focus to a textbox after mouse button click

Markus Freitag 3,786 Reputation points
2020-08-05T06:28:42.077+00:00

Hello,

Is there a way to set the focus on a TextBox control after a mouse click on a button?

Right now, I'm taking the long way around.
Button click.
Delete Textbox
Set the focus because length is 0, but works well.
15743-buttonclick-focus.png

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,691 questions
0 comments No comments
{count} votes

Accepted answer
  1. Markus Freitag 3,786 Reputation points
    2020-08-05T18:19:52.347+00:00

    All is well Peter! I never would have come up with this solution. Looks complicated. That's what I meant.
    I need what you showed.15827-all-good.jpg

    I just wanted to know if you could explain to your solution, why this way and so on.


3 additional answers

Sort by: Most helpful
  1. Peter Fleischer (former MVP) 19,306 Reputation points
    2020-08-05T07:35:26.263+00:00

    Hi Markus,
    in MVVM you can use attaced property to set focus like in following demo:

    15764-x.png
    15728-x2.png
    15744-05-08-2020-09-36-40.gif

    1 person found this answer helpful.
    0 comments No comments

  2. Markus Freitag 3,786 Reputation points
    2020-08-05T14:43:50.937+00:00

    Hi Peter,
    looks good. Thanks!

    Why did you do that static FocusExtension? Can you explain something else?
    So you can get this in xaml, yes. {IsFocused]
    It's hard to come up with that solution.

    0 comments No comments

  3. Peter Fleischer (former MVP) 19,306 Reputation points
    2020-08-05T18:09:06.177+00:00

    Hi Markus,
    Why did you do that static FocusExtension?
    There are many another possible solutions eg. interctivitity.dll. What du you need?

    So you can get this in xaml, yes. {IsFocused]
    I didn't untestand what do you want: Click-Event -> FocusManager?

    0 comments No comments