locked
How to set initial focus to a textbox with gotfocus method in mvvm arch with xaml c# wpf 4.5 RRS feed

  • Question

  • Hi,

    I have create a page/window with many images and a few textbox's and buttons. When the app starts up and the page loads, I want to set the initial focus to the first textbox(the default focus is on images as they are the first loading elements) and change the focus to next elements.

    I see wpf 4.5 only supports gotfocus() and lostfocus() events to set them but with an MVVM arch --> I do not want to have any code-behind till its absolutely necessary and also I  do not want to set the focus in the OnLoaded(). Any suggestions on how to do it?

    Thanks in advance!

    Friday, January 18, 2013 8:39 PM

Answers

  • Hi Ale_dev,

    In WPF, you could use FocusManager in a Page/Window, for example:

            Title="MainWindow"  Height="300" Tag="4545" Width="300" FocusManager.FocusedElement="{Binding ElementName=firstTextBox}">
    
        <Grid Name="grid" Background="Red">
            <Button Content="click"/>
            <TextBox Name="firstTextBox"/>
        </Grid>

    Best regards,


    Sheldon _Xiao
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Joba Diniz Monday, January 21, 2013 10:21 AM
    • Marked as answer by Sheldon _Xiao Monday, January 28, 2013 11:11 AM
    Monday, January 21, 2013 9:39 AM

All replies

  • You could set the Focusable property to false on the images.

    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.

    Friday, January 18, 2013 9:41 PM
  • Hi Ale_dev,

    In WPF, you could use FocusManager in a Page/Window, for example:

            Title="MainWindow"  Height="300" Tag="4545" Width="300" FocusManager.FocusedElement="{Binding ElementName=firstTextBox}">
    
        <Grid Name="grid" Background="Red">
            <Button Content="click"/>
            <TextBox Name="firstTextBox"/>
        </Grid>

    Best regards,


    Sheldon _Xiao
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Joba Diniz Monday, January 21, 2013 10:21 AM
    • Marked as answer by Sheldon _Xiao Monday, January 28, 2013 11:11 AM
    Monday, January 21, 2013 9:39 AM
  • Hi,

    I tried setting the FcousManager property but was not able to find it in WinRT controls. So was not able to set any such properties in XAML.

    Thanks,

    Ale


    • Edited by Ale_dev Friday, February 8, 2013 1:50 AM
    Friday, February 8, 2013 1:18 AM
  • Hi,

    There is no Focusable property in WinRT.

    Thanks,


    Friday, February 8, 2013 1:50 AM