开发者

setting the focus in one user control based on an action in another user control

开发者 https://www.devze.com 2023-01-05 08:47 出处:网络
One user control is a list box where each item in the list has a button. When the button is clicked, editable detail about the item is displayed in another user control. The detail knows what to displ

One user control is a list box where each item in the list has a button. When the button is clicked, editable detail about the item is displayed in another user control. The detail knows what to display and how to display it via data binding to a view model. Both user controls are in the same window.

The problem I am finding tricky here is to set the focus to the first non read only text box.

I have seen one solution here that involves wr开发者_C百科iting a markup extension. It's also a bit old now though, and I am wondering if there is a simpler way to do this.

Cheers,

Berryl

I should have pointed out that the EditCommand that is the trigger for getting the focus to the textbox is in a view model bound to the listing user control. I don't mind having code behind for ui concerns though.

UPDATE Here's a recent nifty post by Josh Smith that addresses part of what the intention is here; to be able to control focus through the ViewModel.


You can use VisualTreeHelper to find the specific textbox control and try textboxcontrol.Focus()


Alright, there may be other ways to do this but because the Josh Smith solution I alluded to in my question uses the Data Binding framework itself, it is a no brainer for use with MVVM. No mucking around with the visual tree or trying to twist your mind around nested controls - simple, elegant, and best of all it works!

BH

0

精彩评论

暂无评论...
验证码 换一张
取 消