Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove underline of dynamic hyperlink in WPF

I create WPF application. In some form user change selected text of richtextbox to hyperlink. I search more than a hour and look for solution. But can't. My dynamic hyperlink is created as follow:

                var textRange = RichTextBox.Selection;
                textRange.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Blue);

                var hyperlink = new Hyperlink(textRange.Start, textRange.End)
                {
                    IsEnabled = true,
                    Foreground = Brushes.Blue
                };

                hyperlink.NavigateUri = new Uri("http://search.msn.com/" + firstOrDefault.WordId);
                var main = new WordMain();
                hyperlink.Click += new RoutedEventHandler(main.hyperLink_Click);
                RichTextBox.IsDocumentEnabled = true;
                RichTextBox.IsReadOnly = false;

How can I remove underline of dynamic hyperlink. I want to use textdecoration, but can't do it by codes.

like image 347
Elvin Mammadov Avatar asked Jan 23 '16 16:01

Elvin Mammadov


2 Answers

For anyone arriving here from search results looking for the simple answer, and doesn't care to retain any decoration (e.g. when using an image):

<Hyperlink ... TextDecorations="">
   ...
</Hyperlink>
like image 91
Chris Avatar answered Sep 29 '22 03:09

Chris


I've just tried this and it worked
Xaml

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid x:Name="Root">
        <TextBlock x:Name="TextBl"></TextBlock>
    </Grid>
</Window>

Code behind

Run run = new Run("Stackoverflow");
Hyperlink hyper = new Hyperlink(run);
hyper.NavigateUri = new Uri("http://stackoverflow.com");
hyper.TextDecorations = null;
TextBl.Inlines.Add(hyper);
like image 25
lyz Avatar answered Sep 29 '22 03:09

lyz