Clicking HyperLinks in a RichTextBox without holding down CTRL - WPF

后端 未结 6 1092
鱼传尺愫
鱼传尺愫 2020-11-30 11:35

I have a WPF RichTextBox with isReadOnly set to True. I would like users to be able to click on HyperLinks contained within the RichTextBox, withou

相关标签:
6条回答
  • 2020-11-30 11:47

    Have you tried handling the MouseLeftButtonDown event instead of the Click event?

    0 讨论(0)
  • 2020-11-30 11:55

    I changed EventSetter from @hillin's answer. MouseLeftButtonDown didn't work in my code (.Net framework 4.5.2).

    <EventSetter Event="RequestNavigate" Handler="Hyperlink_RequestNavigate" />
    
    private void Hyperlink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
    {
        Process.Start(e.Uri.ToString());
    }
    
    0 讨论(0)
  • 2020-11-30 11:56

    JHubbard80's answer is a possible solution, it's the easiest way if you do not need the content to be selected.

    However I need that :P here is my approach: set a style for the Hyperlinks inside the RichTextBox. The essential is to use a EventSetter to make the Hyperlinks handling the MouseLeftButtonDown event.

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

    And in codebehind:

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

    Thanks to gcores for the inspiaration.

    0 讨论(0)
  • 2020-11-30 12:00

    I found a solution. Set IsDocumentEnabled to "True" and set IsReadOnly to "True".

    <RichTextBox IsReadOnly="True" IsDocumentEnabled="True" />
    

    Once I did this, the mouse would turn into a 'hand' when I hover over a text displayed within a HyperLink tag. Clicking without holding control will fire the 'Click' event.

    I am using WPF from .NET 4. I do not know if earlier versions of .NET do not function as I describe above.

    0 讨论(0)
  • 2020-11-30 12:02

    Managed to find a way around this, pretty much by accident.

    The content that's loaded into my RichTextBox is just stored (or inputted) as a plain string. I have subclassed the RichTextBox to allow binding against it's Document property.

    What's relevant to the question, is that I have an IValueConverter Convert() overload that looks something like this (code non-essential to the solution has been stripped out):

    FlowDocument doc = new FlowDocument();
    Paragraph graph = new Paragraph();
    
    Hyperlink textLink = new Hyperlink(new Run(textSplit));
    textLink.NavigateUri = new Uri(textSplit);
    textLink.RequestNavigate += 
      new System.Windows.Navigation.RequestNavigateEventHandler(navHandler);
    
    graph.Inlines.Add(textLink);
    graph.Inlines.Add(new Run(nonLinkStrings));
    
    doc.Blocks.Add(graph);
    
    return doc;
    

    This gets me the behavior I want (shoving plain strings into RichTextBox and getting formatting) and it also results in links that behave like a normal link, rather than one that's embedded in a Word document.

    0 讨论(0)
  • 2020-11-30 12:07

    If you want to turn Arrow into a Hand cursor always without default system navigation, below is the approach.

    <RichTextBox>
                <RichTextBox.Resources>
                    <Style TargetType="{x:Type Hyperlink}">                                
                        <EventSetter Event="MouseEnter" Handler="Hyperlink_OnMouseEnter"/>
                    </Style>                
                </RichTextBox.Resources>
    </RichTextBox>
    
    
    private void Hyperlink_OnMouseEnter(object sender, MouseEventArgs e)
            {
                var hyperlink = (Hyperlink)sender;
                hyperlink.ForceCursor = true;
                hyperlink.Cursor = Cursors.Hand;
            }
    
    0 讨论(0)
提交回复
热议问题