c# - How to autofocus textbox on TrayPopup in WPF? -


i working on application taskbaricon hardcodet. when activate customtraypopup, have customtextbox focused. normal way within xaml doesn't seem work.

here's code:

<usercontrol [...]>     <grid x:name="grid">         <border [...]>             [...]         </border>         <textbox [...]                  focusmanager.focusedelement="{binding relativesource={relativesource self}}"                  > [...]                 <textbox.template>                     [...]                 </textbox.template>             </textbox>         <listbox [...]>[...]         </listbox>     </grid> </usercontrol> 

is there way make textbox autofocus when calling traypopup? doesn't matter if xaml or code behind.

update 1: customtextbox consists of button , textfield. here's whats inside of textbox.template

<controltemplate>   <border [...]>     <visualstatemanager.visualstategroups>[...]     </visualstatemanager.visualstategroups>     <dockpanel>       <button [...]>         <image [...]/>         <button.style>[...]         </button.style>       </button>       <scrollviewer margin="0"                     x:name="part_contenthost"/>     </dockpanel>   </border> </controltemplate> 

is possible, focusmanager can not handle button inside textbox?

update 2: added onclick method button inside customtextbox, text selected , part_contenthost gets focused. doesn't work when try inside onloaded method oder others alike.

update 3: here complete template textbox, in case matters.

<textbox keydown="searchbox_keydown"                  x:name="searchbox"                  width="160"                   height="20"                   margin="10,10,150,130"                  snapstodevicepixels="true"                  overridesdefaultstyle="true"                  foreground="#ffffffff"                  text="some"                  >                  <textbox.caretbrush>                     <solidcolorbrush color="#ff997137"/>                 </textbox.caretbrush>                 <textbox.template>                     <controltemplate>                         <border x:name="border"                             margin="1"                             cornerradius="2"                             borderthickness="0,0,0,1.5"                             borderbrush="#ff997137">                             <visualstatemanager.visualstategroups>                                 <visualstategroup x:name="commonstates">                                     <visualstate x:name="normal"/>                                     <visualstate x:name="disabled">                                         <storyboard>                                             <coloranimationusingkeyframes storyboard.targetname="border"                                                                       storyboard.targetproperty="(panel.background).(solidcolorbrush.color)">                                                 <easingcolorkeyframe keytime="0"                                                                  value="#ff8f8f8f"/>                                             </coloranimationusingkeyframes>                                         </storyboard>                                     </visualstate>                                     <visualstate x:name="readonly">                                         <storyboard>                                             <coloranimationusingkeyframes storyboard.targetname="border"                                                                       storyboard.targetproperty="(panel.background).(solidcolorbrush.color)">                                                 <easingcolorkeyframe keytime="0"                                                                  value="#ff4b4b4b"/>                                             </coloranimationusingkeyframes>                                         </storyboard>                                     </visualstate>                                     <visualstate x:name="mouseover"/>                                 </visualstategroup>                             </visualstatemanager.visualstategroups>                             <dockpanel>                                 <button borderthickness="0"                                          dockpanel.dock="left"                                          horizontalalignment="right"                                          height="15"                                          width="15"                                         background="#00ffffff"                                         click="button_click">                                 <image source="/resources/searchico.png"                                        margin="2"                                        stretch="fill"/>                                 <button.style>                                     <style targettype="{x:type button}">                                         <setter property="template">                                             <setter.value>                                                 <controltemplate targettype="{x:type button}">                                                     <border background="{templatebinding background}">                                                         <contentpresenter horizontalalignment="center" verticalalignment="center"/>                                                     </border>                                                 </controltemplate>                                             </setter.value>                                         </setter>                                         <style.triggers>                                             <trigger property="ismouseover" value="true">                                                 <setter property="background" value="#00ffffff"/>                                             </trigger>                                         </style.triggers>                                     </style>                                 </button.style>                             </button>                                 <scrollviewer margin="0"                                               x:name="part_contenthost"/>                             </dockpanel>                         </border>                     </controltemplate>                 </textbox.template>             </textbox> 

you can use isvisiblechanged event set focus on textbox. sets focus not when view or usercontrol loads, if changing views way of visibility. if try set through loaded event, event may not fire in case of hide , show. changing focus of control on visibilitychange helpful fire.

here example set focus on textbox through code behind in usercontrol

public partial class usercontrol1 : usercontrol     {         public usercontrol1()         {             initializecomponent();              this.isvisiblechanged += usercontrol1_isvisiblechanged;         }          void usercontrol1_isvisiblechanged(object sender, dependencypropertychangedeventargs e)         {                     txt.focus();          //txt name of textbox focused         }     } 

Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -