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

Add values to dropdownlist thats not represented in the db and still get selected value asp.net
Powershell TCP Send Entire File
WPF XML update and treeview Update(UI)
7z decode file that create by System.IO.Compression.GZipStream failed
How To bind A dropdownlist from enum in asp.net MVC using C# [duplicate]
How to fix html tags(which is missing the <open> & <close> tags) with HTMLAgilityPack
c# reading from a text file
Why Request is null in my controller?
C# member parameter as expression and extract the string name
How to disable a Button in ASP.Net
How to download large xml file without timeout exception
Firing the event B after finishing the event A
Update parent control when the child control is manually removed in the designer
ManualResetEvent not working; Thread
IsNothing() in VS2012 SSRS report causes an error
How could I convert a value that looks like a double into an unsigned long, but properly?

Categories

HOME
gulp
redux
netty
primefaces
sip
qemu
jenkins-pipeline
azure-eventhub
highmaps
mifare
nltk
crash
opc-ua
msbi
confluence-rest-api
checkpoint
gnuradio
handlebars.js
subprocess
coding-style
google-search-appliance
decoding
xmlhttprequest
watson-dialog
lambda-calculus
dompdf
bookmarklet
civicrm
gpo
descriptor
android-navigation-drawer
klipfolio
opengraph
struts
formtastic
resx
launchd
federated
delphi-2010
edirectory
apm
urhosharp
qt-designer
rtos
headphones
azure-vm-scale-set
lex
php-mongodb
piranha-cms
reqif
anonymous-function
serialversionuid
ear
appcmd
sharpssh
ajv
webmatrix
try-catch-finally
critical-section
wicketstuff
was
pyautogui
blitz3d
addressbook
racket-student-languages
dimensionality-reduction
init
nimbus
angularfire
php-java-bridge
kik
user-profile
archiva
chomsky-normal-form
salesforce-service-cloud
video-embedding
jersey-test-framework
nslocalizedstring
yfiles
android-facebook
jcomponent
recode
libgcrypt
jts
android-togglebutton
consul-template
teamwork
sicstus-prolog
mri
quickfixn
information-hiding
fxmlloader
sat4j
disparity-mapping
xpand
cs193p
jsmpp
power-management
connections
8-puzzle
fade
static-class
xrandr
generator-expression
microsoft-speech-platform
boost.build
algol
cron4j
correctness
motordriver
big-endian
states
systrace
bindinglist
yii-cactiverecord
jquery-click-event
mass-emails
mediastreamsource
nstoolbar
morfik
manuals
ihtmldocument2
opengl-to-opengles
expander

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile