【问题标题】:Hand Cursor over Hyperlinks in WPF RichTextBox?WPF RichTextBox中超链接的手光标?
【发布时间】:2020-10-14 00:56:15
【问题描述】:

我有一个 RTB,想通过超链接将鼠标光标更改为 MouseEnter 上的手。按下 Ctrl 时似乎默认为这个。我想要它只是当用户将鼠标悬停在超链接上时,无论 Ctrl 是否按下。

超链接当前从MouseLeftButtonDown 事件触发到网络浏览器,效果很好:

   <RichTextBox.Resources>                               
        <Style TargetType="Hyperlink">
           <Setter Property="TextDecorations" Value="Underline"/>
           <Setter Property="Foreground" Value="Black"/>
           <Setter Property="Cursor" Value="Hand"/>
           <EventSetter Event="MouseLeftButtonDown" Handler="Hyperlink_MouseLeftButtonDown" />                                                         
          </Style>                                                            
     </RichTextBox.Resources>

后面的代码:

  private void Hyperlink_MouseLeftButtonDown(object sender, System.Windows.Input.MouseEventArgs e)
  {          
     var hyperlink = (Hyperlink)sender;
     Process.Start(hyperlink.NavigateUri.ToString());                                         
  }

事件按预期触发,它只是我想要的 MouseEnter 上的手形光标。我可以在同一个Style TargetType='Hyperlink'&gt; 上进行两个单独的事件吗?

P.s RTB 格式设置为 Rtf。 谢谢

【问题讨论】:

    标签: c# wpf hyperlink richtextbox


    【解决方案1】:

    通过将事件更改为PreviewLeftMoseButtonDown 解决了这个问题:

      <RichTextBox.Resources>                               
         <Style TargetType="Hyperlink">
           <EventSetter Event="PreviewMouseLeftButtonDown" Handler="Hyperlink_PreviewMouseLeftButtonDown" />                                   
          </Style>                                                            
      </RichTextBox.Resources>
    

    后面的代码:

        private void Hyperlink_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (Keyboard.IsKeyDown(Key.LeftCtrl))
            {
                var hyperlink = (Hyperlink)sender;
                Process.Start(hyperlink.NavigateUri.ToString()); 
            }         
        }
    

    这会导致在按下 Ctrl 键时光标变为手,并且 Web 浏览器会在单击鼠标按钮时触发链接。我在 WPF .NET4.5 中使用扩展的 WPF 工具箱 RTB,并将 RTB 格式设置为 Rtf,结果可能与其他工具不同。

    【讨论】:

      猜你喜欢
      • 2013-05-06
      • 2020-07-20
      • 1970-01-01
      • 1970-01-01
      • 2017-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多