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

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.

Answers


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


Need Your Help

Emulate Python's random.choice in .NET

c# .net random

Python's module 'random' has a function random.choice

Flash Actionscript3: How to reset datagrid between frames

actionscript-3 datagrid flash-cs5.5

I use ActionScript3 in Flash Professional to build an animation in multiple frames.