wpf超链接中的KeyDown事件

发布于 2024-10-21 23:23:46 字数 299 浏览 8 评论 0原文

我需要对 wpf 超链接上的 keydown 事件执行一些操作。

我有一个简单的富文本框,其中有一个超链接。我希望仅当焦点位于超链接上(即光标位于超链接文本上)时才触发 Keydown 事件。

这样做是行不通的,而且我找不到任何解释为什么这行不通。

<Hyperlink KeyDown="Hyperlink_KeyDown">
   test
</Hyperlink>

如果您能帮助我,我将非常感激。

谢谢。 祝你有美好的一天, 阿斯蒂格。

I need to perform some operations on the keydown event on a wpf hyperlink.

I have a simple richtextbox in which i have a hyperlink. I want Keydown event to be fired only when the focus is on the hyperlink, that is the cursor is on the hyperlink text.

Doing this doesn't work and i couldn't find any explanation of why this doesn't work.

<Hyperlink KeyDown="Hyperlink_KeyDown">
   test
</Hyperlink>

I would really appreciate it if you could help me.

Thanks.
Have a good day,
Astig.

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

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

发布评论

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

评论(1

遗忘曾经 2024-10-28 23:23:46

它不起作用,因为超链接无法像聚焦一样被识别,您可以在父控件(例如网格)中捕获此事件,但在捕获它之前,您必须单击它。

因此,您可以像这样捕获窗口的 keydown 事件:

XAML:

<Window x:Class="WpfApplication3.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    Name="MW" KeyDown="MW_KeyDown">
<Grid>
    <TextBlock>
        <Hyperlink Name="HL1" NavigateUri="http://www.google.com/" RequestNavigate="HL1_RequestNavigate">
               Focus it and key down
        </Hyperlink>
    </TextBlock>
</Grid>

和代码:

 private void MW_KeyDown(object sender, KeyEventArgs e)
    {
        if (HL1.IsMouseOver == true)
            HL1_RequestNavigate(HL1,new RequestNavigateEventArgs(HL1.NavigateUri, HL1.Name));
    }

    private void HL1_RequestNavigate(object sender, RequestNavigateEventArgs e)
    {
        Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
        e.Handled = true;
    }

编辑

此外,您还可以将焦点设置为超链接,如下所示:

XAML:

<Hyperlink Name="HL1" NavigateUri="http://www.google.com/" RequestNavigate="HL1_RequestNavigate" KeyDown="HL1_KeyDown" MouseEnter="HL1_MouseEnter">

代码:

private void HL1_RequestNavigate(object sender, RequestNavigateEventArgs e)
    {
        Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
        e.Handled = true;
    }

    private void HL1_KeyDown(object sender, KeyEventArgs e)
    {
        HL1_RequestNavigate(HL1, new RequestNavigateEventArgs(HL1.NavigateUri, HL1.Name));
    }

    private void HL1_MouseEnter(object sender, MouseEventArgs e)
    {
        HL1.Focus();
    }

it doesn't work because hyperlink isn't recognized like focused, you may catch this event in parent control for example grid but before it will be caught you must click on it.

So you may catch window's keydown event like this:

XAML:

<Window x:Class="WpfApplication3.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    Name="MW" KeyDown="MW_KeyDown">
<Grid>
    <TextBlock>
        <Hyperlink Name="HL1" NavigateUri="http://www.google.com/" RequestNavigate="HL1_RequestNavigate">
               Focus it and key down
        </Hyperlink>
    </TextBlock>
</Grid>

and code:

 private void MW_KeyDown(object sender, KeyEventArgs e)
    {
        if (HL1.IsMouseOver == true)
            HL1_RequestNavigate(HL1,new RequestNavigateEventArgs(HL1.NavigateUri, HL1.Name));
    }

    private void HL1_RequestNavigate(object sender, RequestNavigateEventArgs e)
    {
        Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
        e.Handled = true;
    }

Edit

Also you can set focus to hyperlink like that:

XAML:

<Hyperlink Name="HL1" NavigateUri="http://www.google.com/" RequestNavigate="HL1_RequestNavigate" KeyDown="HL1_KeyDown" MouseEnter="HL1_MouseEnter">

code:

private void HL1_RequestNavigate(object sender, RequestNavigateEventArgs e)
    {
        Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
        e.Handled = true;
    }

    private void HL1_KeyDown(object sender, KeyEventArgs e)
    {
        HL1_RequestNavigate(HL1, new RequestNavigateEventArgs(HL1.NavigateUri, HL1.Name));
    }

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