c# - How to find currently focused ListBox -


sometimes find wpf bit frustrating - there way find active listbox in usercontrol usercontrol itself?

what want have property in usercontrol returns listbox focused within usercontrol.

i've tried this:

public listbox focusedlistbox {     if (listbox1.isfocused)         return listbox1;     if (listbox2.isfocused)         return listbox2;     return null; } 

this doesn't work. neither this:

public listbox focusedlistbox {     if (focusmanager.getfocusedelement(this) == listbox1)         return listbox1;     if (focusmanager.getfocusedelement(this) == listbox2)         return listbox2;     return null; } 

or this:

public listbox focusedlistbox {     if (keyboard.focusedelement == listbox1)         return listbox1;     if (keyboard.focusedelement == listbox2)         return listbox2;     return null; } 

so how do this??


based on jason boyd's answer found solution. , must little intuitive... -.-

public listbox focusedlistbox {         {         var currentobject = keyboard.focusedelement dependencyobject;          while (currentobject != null && currentobject != && currentobject != application.current.mainwindow)         {             if (currentobject == listbox1|| currentobject == listbox2)             {                 return currentobject listbox;             }             else             {                 currentobject = visualtreehelper.getparent(currentobject);             }         }          return null;     } } 

what this:

public listbox focusedlistbox() {     dependencyobject currentobject = (uielement)focusmanager.getfocusedelement(this);      while(currentobject != application.current.mainwindow)     {         if(currentobject == listbox1 || currentobject == listbox2)         {             return currentobject listbox;         }         else         {             currentobject = logicaltreehelper.getparent(currentobject);         }     }      return null; } 

the reason walking logical tree is (more likely) not listbox has focus child object of listbox.


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

order - Notification for user in user account opencart -