question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RichTextBox.Selection.GetPropertyValue(FontStyleProperty) returns DependencyProperty.UnsetValue for Hyperlink with Italic style

See original GitHub issue
  • .NET Core Version: .NET 6.0.9
  • Windows version: Windows 10 21H2 (19044.2006)
  • Does the bug reproduce also in WPF for .NET Framework 4.8?: Yes

Problem description:

We use RichTextBox.Selection.GetPropertyValue(FontStyleProperty) to determine the selected text’s style. The issue is, if the selected text is Hyperlink and it’s fully selected, the returned style is incorrect if it’s not default.

As an example for the repro below, the button’s color depends on whether the selected text is Italic or not (blue if yes, otherwise red). By pressing the button the selected text’s style is changed to/from Italic.

Here nothing is selected, the whole hyperlink’s text is Italic.

image

Here only a part of the hyperlink is selected, the button is blue.

image

The whole hyperlink is selected, the button is red (even though the whole text is Italic).

image

This happens because when the whole text is selected GetPropertyValue(FontStyleProperty) returns DependencyProperty.UnsetValue.

Actual behavior:

GetPropertyValue(FontStyleProperty) returns DependencyProperty.UnsetValue for Hyperlink if has Italic font style.

Expected behavior:

GetPropertyValue(FontStyleProperty) should return FontStyles.Italic for Hyperlink if has Italic font style.

Minimal repro:

https://github.com/vonzshik/WpfRichTextBoxItalicLink

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
miloushcommented, Oct 10, 2022

No, this happens as long as the starting position is included in the selection (i.e. even for the first character only). The reason is because the parent the property is read from is the Paragraph and the paragraph does not have italic set - basically the cursor is not inside a run, like this:

<Paragraph>|<Hyperlink><Run Text="https://google.com"/></Hyperlink></Paragraph>
0reactions
vonzshikcommented, Nov 10, 2022

This submission has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 14 days.

I’m not sure what feedback is expected from me, but I would prefer for this issue to be fixed at some point 🐝

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using together SelectionChanged event of RichTextBox ...
I tested that without those two lines, the app successfully changes the font size of any selected text (even if it has words...
Read more >
RichTextBox.Selection Property (System.Windows.Controls)
A TextSelection that represents the selected text in the RichTextBox. Examples. The following example shows how you can apply bold, italic, and ...
Read more >
Getting Started with Windows Presentation Foundation in . ...
Learn how build desktop applications for Windows 10 using .NET Core Windows Presentation Foundation (WPF) with this step-by-step tutorial.
Read more >
WPF - David Rodrigues Blog
However, because my hyperlinks will always list the URI and never a user typed name, detecting hyperlinks as the user types and creating...
Read more >
TextSelection.cs source code in C# .NET
Controls; /// /// The TextSelection class encapsulates selection state for the RichTextBox /// control. It has no public constructor, but is exposed via...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found