views:

61

answers:

2

Hello

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 writing 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.

+1  A: 

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

Ragunathan
@Rag. I'm not familiar with the VisualTreeHelper class, but it does sound useful (is this a framework class for both wpf and silverlight?). Not sure where that would be in this case, and how the EditCommand in the view model might use it though. Does this make sense to you?
Berryl
I don't think EditCommand set the focus. See the following linkshttp://stackoverflow.com/questions/636383/wpf-ways-to-find-controlshttp://www.codeproject.com/KB/WPF/WpfElementTrees.aspx
Ragunathan
A: 

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

Berryl