Initiating UserControl via MVVM in WPF / focus issue.

Posted by benndev on Stack Overflow See other posts from Stack Overflow or by benndev
Published on 2010-06-12T11:34:45Z Indexed on 2010/06/12 11:43 UTC
Read the original article Hit count: 742

Filed under:
|
|
|

Hi there

I have a few usercontrols loaded into a tabcontrol via MVVM in WPF.

Within the XAML for the usercontrol I am setting focus to a textbox using the FocusManager, however this appears to only work when the first instance of the usercontrol is created.

Just to test I added a loaded event handler to the usercontrol - this is only called on the first instance.

I'm using data templates for the user controls as follows:

    <DataTemplate DataType="{x:Type local:UserTypeViewModel}">
        <local:UserTypeView />
    </DataTemplate>

The textbox is focused as follows:

    FocusManager.FocusedElement="{Binding ElementName=txtName}"

Additionally I'm using a global event handler (for the textbox GotFocus event) which selects all the text using a dispatcher.

If anyone has any tips on how to achieve focus with every usercontrol I'd be very grateful.

Thanks, Ben.

© Stack Overflow or respective owner

Related posts about wpf

Related posts about mvvm