I've hit this problem a couple of times now and it always seems unnecessarily hard.. how are others setting the initial focus to items that are generated through a DataTemplate? Here's the issue -- I have an application that dynamically builds up a set of fields and uses an ItemsControl to display them. The ItemsControl uses a template like:
<DataTemplate>
<Label ...>
<TextBox ....>
</DataTemplate>
and I'd like the initial focus to be placed into the first TextBox in the ItemsControl. I can do it in code behind by grabbing the Item generator and locating the first TextBox and forcing focus there, but there's got to be a better way -- ideally a way to specify it in XAML directly. I've worked hard to try to keep the UI and code separated and it seems anti-WPF to have to know it's being rendered in an ItemsControl. I've tried FocusManager but it consistently ignores me -- I suspect because the template hasn't expanded yet.
I wonder if I could create a "initial focus" attached behavior to solve this in a once-for-all style .. I might pursue that next..
Thanks for any ideas!
mark