c# - Clicking on a Label to focus another control in WPF -


i have taken break wpf year , stumped simple problem. swear there easy way tell label focus control when clicked.

 <stackpanel>     <label target="textbox1">label text</label>     <textbox name="textbox1" /> </stackpanel> 

when user clicks on "label text" want textbox receive focus. possible?

you should make use of target property:

<label content="_stuff:" target="{x:reference textbox1}"        mouseleftbuttonup="label_mouseleftbuttonup"/> <textbox name="textbox1" /> 
private void label_mouseleftbuttonup(object sender, mousebuttoneventargs e) {     if (e.clickcount == 1) //note lie, not check "real" click     {         var label = (label)sender;         keyboard.focus(label.target);     } } 

the whole point of using label in first place instead of textblock make use of associative functionality, see reference on msdn.

about note, asked question how real click over here, if curious.


Comments