单击标签以聚焦 WPF 中的另一个控件

发布于 2024-11-04 03:53:24 字数 271 浏览 1 评论 0原文

我已经离开 WPF 大约一年了,我被这个简单的问题难住了。我发誓有一种简单的方法可以告诉标签在单击时将焦点集中到另一个控件。

 <StackPanel>
    <Label Target="TextBox1">Label Text</Label>
    <TextBox Name="TextBox1" />
</StackPanel>

当用户单击“标签文本”时,我希望文本框接收焦点。这可能吗?

I have taken a break from WPF for about a year and I am stumped by this simple problem. I swear there was an easy way to tell a label to focus to another control when it is clicked.

 <StackPanel>
    <Label Target="TextBox1">Label Text</Label>
    <TextBox Name="TextBox1" />
</StackPanel>

When the user clicks on "Label Text" I want the TextBox to receive focus. Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

温折酒 2024-11-11 03:53:24

您应该利用 Target 属性:

<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);
    }
}

首先使用 Label 而不是 TextBlock 的全部目的是利用其关联功能,请参阅 MSDN 上的参考

关于我的笔记,我问了一个有关如何获得真正点击的问题 这里,如果你好奇的话。

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.

无尽的现实 2024-11-11 03:53:24

我找到了我用于此目的的代码,并认为我会分享它,以防它对其他人有用。

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

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();
        }
    }
}
只是我以为 2024-11-11 03:53:24

你不能用快捷键组合来做到这一点吗

    <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> 

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> 
爺獨霸怡葒院 2024-11-11 03:53:24

根据阅读HTML“for”属性的 WPF 标签对应部分,您需要一个附加的行为来做到这一点。

Based on reading WPF label counterpart for HTML "for" attribute, you'd need an attached behavior to do that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文