wpf - How to active UserControl outside the wrapper? -


i met focus related problem in usercontrol:

suppose have usercontrol this:

<usercontrol x:class="_20130826.usercontrol1"              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">     <stackpanel>         <listbox>             <listboxitem>                 <textblock text="text1" />             </listboxitem>             <listboxitem>                 <textblock text="text2" />             </listboxitem>             <listboxitem>                 <textblock text="text3" />             </listboxitem>             <listboxitem>                 <textblock text="text4" />             </listboxitem>         </listbox>     </stackpanel> </usercontrol> 

and mainwindow.xaml this:

<window x:class="_20130826.mainwindow"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         xmlns:temp="clr-namespace:_20130826"         title="mainwindow">     <stackpanel>         <button content="deactive usercontrol" />         <stackpanel>             <button name="button1" content="active usercontrol" />             <contentcontrol>                 <temp:usercontrol1 />             </contentcontrol>         </stackpanel>     </stackpanel> </window> 
  • step 1: click text1 textblock inside usercontrol, , default background changes deeper.

  • step 2: click 'deactive usercontrol' button, text1 background turns lighter.

  • step 3: click 'active usercontrol' button, ...

i want text1 background changed deeper, which means usercontrol has been focused/actived.

how can achieve this?

if want set focus usercontrol when button("active usercontrol") gets pressed, can add click event handler , assign focus usercontrol.

so like:

<window x:class="_20130826.mainwindow"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         xmlns:temp="clr-namespace:_20130826"         title="mainwindow">     <stackpanel>         <button content="deactive usercontrol" />         <stackpanel>             <button name="button1" content="active usercontrol" click="button1_onclick" />             <contentcontrol>                 <temp:usercontrol1 x:name="myusercontrol" />             </contentcontrol>         </stackpanel>     </stackpanel> </window> 

and in mainwindow.xaml.cs:

private void button1_onclick(object sender, routedeventargs e) {   focusmanager.setfocusedelement(this, myusercontrol); } 

now when click "active usercontrol" button, focus switch usercontrol. not give "deeper" state on listboxitem still doesnt have focus. sort that

in usercontrol1.xaml.cs add:

protected override void ongotfocus(routedeventargs e) {   base.ongotfocus(e);    if (!equals(e.originalsource, this))     return;   traversalrequest trequest = new traversalrequest(focusnavigationdirection.next);   movefocus(trequest); } 

what when usercontrol gets focus, moves focus next element within element clicked initially.

you can demo of code tweaked this: here


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -