Domanda

I have a RTB and want to change the mouse cursor to a hand on MouseEnter over hyperlinks. It seems to default to this with Ctrl down. I want it just when the user hovers over the hyperlink regardless of whether Ctrl is down or not.

The hyperlink is currently fired to the web browser from a MouseLeftButtonDown event which works great:

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

Code behind:

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

The event fires as intended, it's just the hand cursor I want on MouseEnter. Can I have two seperate events on the same Style TargetType='Hyperlink'>?

P.s The RTB format is set to Rtf. Thanks

È stato utile?

Soluzione

Solved this by changing the event to a PreviewLeftMoseButtonDown:

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

Code behind:

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

This results in the cursor changing to hand on Ctrl down and the web browser firing the link on mouse button click. I'm using the Extended WPF ToolBox RTB in WPF .NET4.5 & with the RTB formatting to Rtf to results may differ with other tools.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top