stukselbax
stukselbax

Reputation: 5935

How to set focus to TextBox without event handler

I want HyperLink.Click event set TextBox.IsFocused to true. I'm using mvvm, and I'm intrested to avoid any code-behind in my View, so I looking for solution without event handler of HyperLink.Click event. I need to design Commmand, but properties IsFocused, IsKeyboardFocused, IsKeyboardFocusWithin are readonly... So I found solution - I can set CommandParameter={Binding ElementName=MyTextBox}. But this way my ICommand implementation retrive UI element. Is it allowably with MVVM pattern to get UI element as CommandParameter? Is there another way to achive this without View code-behind?

Upvotes: 1

Views: 670

Answers (1)

Souvik Basu
Souvik Basu

Reputation: 3139

Create an Attached Behavior to set focus on the UIElement. see here on how to create behaviors

Make the AttachedBehavior bind to IsFocued Property in Viewmodel of the AssociatedObject.

Whenever the viewmodel changes the IsFocused property to true (through some command in viewmodel), the attached behavior will come to know through data binding and call Focus() in the UIElement.

Upvotes: 2

Related Questions