Clicking on a Label to focus another control in WPF Clicking on a Label to focus another control in WPF wpf wpf

Clicking on a Label to focus another control in WPF


You should make use of the 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 that this is a lie, this does not check for a "real" click    {        var label = (Label)sender;        Keyboard.Focus(label.Target);    }}

The whole point of using a Label in the first place instead of a TextBlock is to make use of its associative functionality, see the reference on MSDN.

About my note, i asked a question about how to get a real click over here, if you are curious.


I found the code I used to use for this and figured I would share it in case it is useful for anyone else.

public class LabelEx : Label{    protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)    {        if (Target != null)        {            Target.Focus();        }    }}


can't you do that with the shortcut key combination

    <Grid>    <Grid.ColumnDefinitions>        <ColumnDefinition Width="Auto"></ColumnDefinition>        <ColumnDefinition></ColumnDefinition>    </Grid.ColumnDefinitions>    <Label Target="{Binding ElementName=textbox1}" Content="_Name"/>    <TextBox Name="textbox1" Height="25" Grid.Column="1" VerticalAlignment="Top"/></Grid>