c# - IsDefault and FocusScope recovery -


my problem have 2 buttons: "backward" , "forward".

  <stackpanel>             <button content="backward"/>             <button content="forward" isdefault="true"/>         </stackpanel> 

now when "enter" button clicked works fine. there possible flow user clicks backward button , wants use "enter" use "forward" button. problem "backward" button focused "backward" button handles "enters". tried unfocus "backward" button after click , still doesn't work because stackpanel loses it's focus scope. way of recovering focus scope stackpanel?

in order words: after clicking backward button want set "forward" button "isdefaulted" property true. , don't want have whole stackpanel focused, want change focus scope focus scope forward button placed.

there different things can achieve looking for. however, these each affect user experience in way may or may not desirable.

  • you write handler "backward" button click sets focus "forward" button. downside of user cannot choose "backward" button , keep pressing space activate multiple times.

    xaml:

    <button content="backward" click="button_click" /> <button x:name="forwardbutton" content="backward" /> 

    code:

    private void button_click(object sender, routedeventargs e) {     forwardbutton.focus(); } 
  • another option set "backward" not focusable. means button cannot reached user @ using keyboard, forced use mouse activate (making application less accessible, bad).

    <button content="backward" focusable="false" /> 
  • alternatively, set "backward" button not handle return/enter key. still focused when clicked on, , pressing space still activate after point. however, pressing enter ignored, allowing default button pick up. confusing user though since button have focused not 1 being activated. in fact, tested , both buttons appeared visually focused @ same time after clicked "backward" (i using aero theme).

    <button content="backward" keyboardnavigation.acceptsreturn="false" /> 
  • yet solution manually handle key presses directly in code behind using previewkeydown handler , perform desired operations, suggested in answer toby crawford. requires more manual work, allows complete control on happens. solution, need come way indicate user happening. also, there confusing flows such user tabbing "backward" button, pressing enter, , having "forward" button activate. need account things that.

basically, boils down desired user experience. sort of thing have decide user input flows more important @ expense of having less important ones potentially not work expected.

focus management in wpf complex 1 2. can whatever want (whether makes sense user or not), helps plan overall focus flow of application. should consider reading on how focus , keyboard focus work in wpf.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -