Remove underline of dynamic hyperlink in WPF
Asked Answered
L

3

15

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.

Litta answered 23/1, 2016 at 16:47 Comment(3)
I'm sorry, but there's something that I didn't understand: Do you have to keep any other TextDecoration? Or can you just remove them all?Wise
@Elvin Mammadov: Should all hyperlinks in your application have the same formatting?Tithing
Yes. I add these hyperlink to database, with @lyz's codes work when I add text to richtextbox. but after retrieve from database hyperlink underline come againLitta
A
10

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);
Allison answered 23/1, 2016 at 17:53 Comment(4)
but When I load it from database the underline come againLitta
What is your textRange and to which element do you add the hyperlink ? if you can show more codeAllison
And to which element do you add the hyperlink please ? I don't see it in the code you updatedAllison
there is not other element.Litta
S
31

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>
Scabious answered 28/3, 2017 at 15:23 Comment(0)
A
10

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);
Allison answered 23/1, 2016 at 17:53 Comment(4)
but When I load it from database the underline come againLitta
What is your textRange and to which element do you add the hyperlink ? if you can show more codeAllison
And to which element do you add the hyperlink please ? I don't see it in the code you updatedAllison
there is not other element.Litta
T
6

Remove the color formatting from your C# code and put this in your App.xaml file:

<Application.Resources>
    ...
    <TextDecorationCollection x:Key="_textDeco_hyperlink">
        <!--<TextDecoration Location="Underline" />-->
    </TextDecorationCollection>

    <Style TargetType="{x:Type Hyperlink}">
        <Setter Property="Foreground" Value="Black"/>
        <Setter Property="Background" Value="Khaki" />
        <Setter Property="ForceCursor" Value="True" />
        <Setter Property="Cursor" Value="Hand" />
        <Setter Property="TextDecorations" Value="{DynamicResource _textDeco_hyperlink}" />
        <Setter Property="IsEnabled" Value="False" />
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Foreground" Value="Khaki" />
                <Setter Property="Background" Value="Black" />
            </Trigger>
        </Style.Triggers>
    </Style>
    ...
<Application.Resources>

If this is not working I think it's the way how you serialize and deserialize your FlowDocument. Try this:

// Store your FlowDocument as a list of strings
List<string> blocksAsStrings = FlowDoc_store(_docSelected._Rtb.Document);
...
// Load your FlowDocument into your RichTextBox
rtb.Document = FlowDoc_load(blocksAsStrings);

/// <summary>
/// Stores a FlowDocument as a list of strings, each string represents a Block.
/// </summary>
public static List<string> FlowDoc_store(FlowDocument flowDoc)
{
    List<string> blocksAsStrings = new List<string>(flowDoc.Blocks.Count);

    foreach (Block block in flowDoc.Blocks)
    {
        blocksAsStrings.Add(XamlWriter.Save(block));
    }

    return blocksAsStrings;
}

/// <summary>
/// Loads a FlowDocument from a list of strings, each string represents a Block.
/// </summary>
public static FlowDocument FlowDoc_load(List<string> blocksAsStrings)
{
    FlowDocument flowDoc = new FlowDocument();

    foreach (string blockAsString in blocksAsStrings)
    {
        using (StringReader stringReader = new StringReader(blockAsString))
        {
            using (XmlReader xmlReader = XmlReader.Create(stringReader))
            {
                Block block = (Block)XamlReader.Load(xmlReader);
                flowDoc.Blocks.Add(block);
            }
        }
    }

    return flowDoc;
}
Tithing answered 25/1, 2016 at 9:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.