Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Problem Setting Focus

Problem Setting Focus

Scheduled Pinned Locked Moved WPF
wpfhelpwcf
2 Posts 2 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    I have a simple login window with Company, User Name, and Password. There is a Remember Me checkbox, and if the user checks it, then I pre-fill in the Company and User Name the next time they run the app. In that case, I want to set focus to the Password field. In the Window's XAML I have

    FocusManager.FocusedElement="{Binding FocusedElementName}"

    Then in the VM I set the FocusedElementName property to either "company" or "password." The problem is that the focus is not being set. The code behind is working as expected.

    If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

    Richard DeemingR 1 Reply Last reply
    0
    • K Kevin Marois

      I have a simple login window with Company, User Name, and Password. There is a Remember Me checkbox, and if the user checks it, then I pre-fill in the Company and User Name the next time they run the app. In that case, I want to set focus to the Password field. In the Window's XAML I have

      FocusManager.FocusedElement="{Binding FocusedElementName}"

      Then in the VM I set the FocusedElementName property to either "company" or "password." The problem is that the focus is not being set. The code behind is working as expected.

      If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      The problem is that you're setting the Path of the binding. The FocusedElement property relies on the ElementName of the binding:

      FocusManager.FocusedElement Attached Property (System.Windows.Input) | Microsoft Docs[^]:

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

      You can't bind the properties of a binding, so you can't do this:

      {Binding ElementName={Binding ...}}

      Short of using the code-behind for the view, the simplest option is probably to use a style with a series of data triggers, as shown in this SO answer[^]:

      <Style>
      <Style.Triggers>
      <DataTrigger Binding="{Binding FocusedElement}" Value="First">
      <Setter Property="FocusManager.FocusedElement" Value="{Binding ElementName=txtbox1}"/>
      </DataTrigger>
      </Style.Triggers>
      </Style>


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups