c#


Detecting underline in selected WPF RichTextBox control from generated XAML code


Full repro: https://github.com/Myrmex/wpf-reprounderline. Please see instructions below.
I'm using a RichTextBox control in a WPF app getting its content from programmatically generated XAML code. In this control, I need to place toggle buttons for bold, italic, and underline, and update their toggle status whenever the user's selection changes, so that they reflect the status of selected text.
This works fine, except for a detail: detecting underline from the selected text (or just the text under the caret, when the selection is empty) does NOT work UNLESS I replace Span with Run. I can see the underline decoration displayed in both cases in the control, but if I place the caret inside it, it does not reflect this decoration: TextDecorations is empty.
This is a sample of the "faulty" generated XAML code:
<FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"><Paragraph><Span Foreground="#FFFF0000">hello</Span> <Span><Span.TextDecorations><TextDecoration Location="Underline" /></Span.TextDecorations>under</Span></Paragraph></FlowDocument>
If I set the content of the WPF control to this code, underline is NOT detected. If I replace Span with Run, it does work, but I cannot do this in real code, as the XAML generator uses Span's rather than Run's to ensure that they can be nested (a Run only allows text children).
I tried with the attribute notation (<Span TextDecorations="Underline">...</Span>), but nothing changes. Whenever I manually underline something, the WPF control emits Run's, so that they work. Yet, it does not when using Span's.
Here is my detection code:
private void OnSelectionChanged(object sender, RoutedEventArgs e)
{
// italic
object temp = _rtb.Selection.GetPropertyValue(TextElement.FontStyleProperty);
_tbbItalic.IsChecked =
temp != DependencyProperty.UnsetValue &&
temp != null &&
temp.Equals(FontStyles.Italic);
// bold
temp = _rtb.Selection.GetPropertyValue(TextElement.FontWeightProperty);
_tbbBold.IsChecked = temp != DependencyProperty.UnsetValue &&
temp != null &&
temp.Equals(FontWeights.Bold);
// underline
temp = _rtb.Selection.GetPropertyValue(Inline.TextDecorationsProperty);
_tbbUnderline.IsChecked = temp != DependencyProperty.UnsetValue &&
temp != null &&
temp.Equals(TextDecorations.Underline);
}
I have prepared a full repro solution: just launch the app and type something, adding bold, italic, underline; then move the caret around, and see how these are reflected in the buttons. You can get the WPF control's XAML code using the Get XAML button. Then load the faulty XAML code using the Load faulty button, and set the WPF control's content using the Set XAML button. Now, if you place the caret inside the underlined text, the underline is not detected. If you replace Span with Run, and set the WPF content again, this works.
Could anyone suggest a solution?

Related Links

C# DataGridView - Detect Cell value Change
C# binding won't work in WPF when using navigation in MVVM Light
Incorrect syntax near parameter (C# with SQL)
C# using static directive not valid
Localized resources - cannot access more than one resource in a .resx file
Socket throws nullreferenceexception while asynchrously receiving data
Error creating a line graph on winform
Is there a way to uniquely identify the website (or application) hosted in IIS?
Windows Service not functioning
Does HttpResponseStream buffers the data that it streams?
Audience URI validation failed. Audience does not match
How can i compare arrays in two jagged arrays C#
Implement RemoveRange in Entity Framework
How to Configure SSL in Quickfix/N
Getting error when trying to scaffhold APIController with foreignKey
Webservice API Nomadesk

Categories

HOME
spring-boot
gcc
ckan
leaflet
compression
casting
c++builder
skypedeveloper
watson-iot
datetimepicker
psexec
travis-ci-cli
doctrine2
minimax
activejdbc
angular-universal
fetch
ndepend
distinct
email-attachments
captcha
mbed
javascript-debugger
game-engine
versioning
project-reactor
aws-devicefarm
passbook
openoffice-basic
uipath
pixels
deployment-descriptor
inspec
knapsack-problem
mongoose-im
utc
resx
setup-project
folder
paper-trail-gem
xlib
arp
drawrect
unity3d-5
eclipse-jdt
asp.net-webhooks
pymunk
javafxports
sdl-2
anypoint-studio
redhat-brms
var
firefox-developer-edition
result
bootstrap-carousel
logan-square
line-endings
anonymous-function
ionic2-select
dmalloc
bipartite
codesniffer
scrollmagic
stringbuilder
visual-web-developer
key-value-store
git-cherry-pick
angularfire
django-celery
sbt-proguard
transaction-isolation
pebble-js
nd4j
wizard
wordpress-plugin
jersey-test-framework
github-for-mac
gnu-sort
roslyn-code-analysis
properties-file
eggplant
transparentproxy
valdr-bean-validation
lwuit
formbuilder
step
nspopupbutton
hotlinking
farpoint-spread
image-preloader
new-operator
mongoid4
logo-lang
xtify
scidb
8-puzzle
fade
playing-cards
wpa
nsautoreleasepool
junit-rule
big-endian
hypertable
waveout
php-gettext
systrace
django-admin-tools
motorola-droid
flexbuilder
xhtmlrenderer
netbeans7.0
datarelation
mediastreamsource
simultaneous
database-agnostic
system-analysis
code-golf
numerical-computing
glassfish-embedded
pkcs#7
morfik
datadude
wtsapi32
managed-code
httpbrowsercapabilities
j++
scalable

Resources

Encrypt Message