4 posts, 0 answers
  1. Jose
    Jose avatar
    1 posts
    Member since:
    Jul 2010

    Posted 18 Jun 2013 Link to this post

    Hi, telerik guys!

    I want to change the Hyperlink Style when I use the RadRichTextBox.InsertHyperlink method, I've created a custom style (see code below) and passed as a parameter to this method instead of the CurrentEditingStyle, but still the text is shown in color blue and underlined, there's any way to change this style and show an specific hyperlink with a custom style? Thanks in advance for the help.

    Example Code:
    Telerik.Windows.Documents.Model.Styles.StyleDefinition mergeFieldStyle = new Telerik.Windows.Documents.Model.Styles.StyleDefinition();
    mergeFieldStyle.BasedOn = radRichTextBox.DefaultStyleSettings.BasedOn;
    mergeFieldStyle.BasedOnName = radRichTextBox.DefaultStyleSettings.BasedOnName;
    mergeFieldStyle.Type = radRichTextBox.DefaultStyleSettings.Type;
    mergeFieldStyle.DisplayName = "MergeFieldStyle";
    mergeFieldStyle.Name = "MergeFieldStyle";
    mergeFieldStyle.SetPropertyValue(Telerik.Windows.Documents.Model.Span.ForeColorProperty, Colors.Red);
    mergeFieldStyle.SetPropertyValue(Telerik.Windows.Documents.Model.Span.UnderlineDecorationProperty, Telerik.Windows.Documents.UI.TextDecorations.DecorationProviders.UnderlineTypes.None);

    radRichTextBox.InsertHyperlink(mergeField, string.Format("[{0}]", caption), mergeFieldStyle);

    I've also try to change the SpanProperties in the CurrentEditingStyle and passing them to the method,  with my custom style properties but no success either.

  2. Deyan
    Deyan avatar
    171 posts

    Posted 21 Jun 2013 Link to this post

    Hello Jose,

    Thank you for contacting us about this issue!

    The overload of InsertHyperlink method taking a StyleDefinition as third argument is now obsolete, so the parameter hyperlinkStyle is not used and the default document hyperlink style will be used instead. That is why in your case you will have to select the inserted hyperlink and set a new style to it. Here is a sample code how this can be done:

    First, you will have to create your style, similarly to your sample code snippet. The only difference is that I'd suggest you use the constructor for StyleDefinition taking StyleType.Character as an argument which is appropriate for applying on hyperlinks.

    StyleDefinition mergeFieldStyle = new StyleDefinition(StyleType.Character);
                mergeFieldStyle.BasedOn = this.radRichTextBox.DefaultStyleSettings.BasedOn;
                mergeFieldStyle.BasedOnName = this.radRichTextBox.DefaultStyleSettings.BasedOnName;
                mergeFieldStyle.DisplayName = "MergeFieldStyle";
                mergeFieldStyle.Name = "MergeFieldStyle";
                mergeFieldStyle.SetPropertyValue(Telerik.Windows.Documents.Model.Span.ForeColorProperty, Colors.Red);

    The next step is to insert the desired hyperlink and after the insertion, select it and change its style with the already predefined custom style. You may notice that in the following sample code all operations are surrounded by BeginUndoGroup and EndUndoGroup methods so that you can reverse the insertion and the change of style property in a single Undo step.
                this.radRichTextBox.InsertHyperlink(new HyperlinkInfo() { Target = HyperlinkTargets.Blank, NavigateUri = "http://www.telerik.com" }, "Telerik link");
                DocumentPosition documentPosition = new DocumentPosition(this.radRichTextBox.Document.CaretPosition);
                var rangeStart = this.radRichTextBox.Document.GetContainingAnnotationRanges<HyperlinkRangeStart>(documentPosition.GetCurrentInline(), true).First();

    If you still have any concerns about this or face any other issue do not hesitate to contact us again!

    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. Kushagra
    Kushagra avatar
    16 posts
    Member since:
    May 2011

    Posted 17 Feb 2014 in reply to Deyan Link to this post

    How would we change the style if the Selection is multiline in which case the Hyperlink extends to may be a table or span etc. Especially in the case when the text is inside a table this.radRichTextBox.Document.GetContainingAnnotationRanges<HyperlinkRangeStart>(documentPosition.GetCurrentInline(), true).Count() comes to 0. Any workarounds regarding this?

    Warm Regards,
  4. Mihail
    Mihail avatar
    265 posts

    Posted 20 Feb 2014 Link to this post

    Hello Kushagra,

     I will try to explain in details what are the most important steps in order to change the style of hyperlink.

    First and most important thing is to get inline which is inside a hyperlink. Such inlines are the hyperlink start and end range, and all the spans that are containing the text of the hyperlink. You can do that by navigating a document position to the required inline. Here is example on how to do that:
    DocumentPosition documentPosition = new DocumentPosition(this.radRichTextBox.Document.CaretPosition);
    There are many convenient methods inside DocumentPosition to navigate around the document. I can recommend you to check them.

    Once you have the required inline you may apply the style to the hyperlink as Deyan already showed. Here is the code sample on how to do it:
    var rangeStart = this.radRichTextBox.Document.GetContainingAnnotationRanges<HyperlinkRangeStart>(inline, true).First();

    If you need to apply this style to all hyperlinks in a document you can do that by iterating over all HyperlinkRangeStart in the document and get the inline from them. Here is code sample on how to do it:
    foreach (var hyperlinkRangeStart in this.radRichTextBox.Document.EnumerateChildrenOfType<HyperlinkRangeStart>())
        var rangeStart = this.radRichTextBox.Document.GetContainingAnnotationRanges<HyperlinkRangeStart>(hyperlinkRangeStart, true).First();

    In case you need to apply the new hyperlink style only to specific hyperlink you can do that by iterate over all HyperlinkRangeStart elements and depending on the information in the hyperlink to apply the style on the hyperlink you want.

    Regarding the case when the hyperlink is inside a table or on multiline, just move the document position to the hyperlink and get the inline needed to apply the style.

    Please do not forget to Begin and End the undo group if you need to be able to revert those changes in the document using the undo command.

    I hope this information answers your questions. If you have further questions or comments feel free to contact us again,

Back to Top