wpf - How to stop FocusManager from Moving the focus outside an open Popup when using IsFocusScope option -


i have 2 controls toolbar , textbox. toolbar has button opens popup button in it.

current behavior: if click inside textbox , becomes focused , click button toolbar opens popup textbox still focused , receives keyboard input.

now know default behavior items inside focusscope toolbar is, don't need behavior when popup open. how can avoid ?

here example:

<window x:class="wpfapplication67.mainwindow"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     title="mainwindow" height="350" width="525"> <window.resources> </window.resources> <stackpanel horizontalalignment="left" width="400">     <toolbar>         <togglebutton name="openbutton">open popup</togglebutton>         <popup focusable="true"             placement="right"                     staysopen="false"              isopen="{binding ischecked, elementname=openbutton, mode=twoway}"             placementtarget="{binding elementname=openbutton}">             <stackpanel                  width="100"                  height="100"                  background="darkgray"                  focusable="true">                     <button>more</button>                 </stackpanel>         </popup>     </toolbar>      <textbox text="set focus on this..." />  </stackpanel> 

edit: i'm striving find explanation moves focus on button click inside nested focusscope , how can stop buttons (like 1 inside popup) doing it.

you have 3 requirements (correct me if wrong):

  1. if pop opened, focus should inside popup i.e. on stackpanel.
  2. on close of popup, focus should retained textbox.
  3. when button inside popup clicked, focus should not leave popup.

let's pick these requirements 1 one.

if pop opened, focus should inside popup i.e. on stackpanel.

like mentioned in comments, put focus on stackpanel on opened event of popup:

private void popup_opened(object sender, eventargs e) {    stackpanel.focus(); } 

on close of popup, focus should retained textbox.

hook closed event , put focus on textbox:

private void popup_closed(object sender, eventargs e) {    textbox.focus(); } 

when button inside popup clicked, focus should not leave popup.

now, comes tricky part. mentioned in comments click on button inside popup, focus moved outside of popup.

what can prevent attach handler previewlostkeyboardfocus event of stackpanel. in handler check condition if keyboard focus within popup, set e.handled = true event gets handled here , no bubble event raised force keyboard focus outside of stackpanel.

that being said in case have textbox inside stackpanel besies button, handling event won't allow move focus within popup well. avoid such situations can check if new focused element doesn't belong within stackpanel handle event.

here's code achieve (add handler on previewlostkeyboardfocus event of stackpanel):

private void stackpanel_previewlostkeyboardfocus(object sender,                                               keyboardfocuschangedeventargs e) {    var newfocusedchild = e.newfocus frameworkelement;    bool ismovingwithinpanel = newfocusedchild != null                               && newfocusedchild.parent == stackpanel;     // if keyboard focus within stackpanel , new focused element    // outside of stackpanel boundaries handle event.    if (stackpanel.iskeyboardfocuswithin && !ismovingwithinpanel)       e.handled = true; } 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -