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

Using regex to remove all newlines within <li> </li>
Using StructureMap with derived interfaces
How to create my own certificate authority for IIS?
How to convert PDF to WORD in c#
Injecting a primitive property in a base class with Castle Windsor
Deleting NHibernate mapped object not working - causes sql UPDATE
How do I create a real-time Excel automation add-in in C# using RtdServer?
Learning XML, what are the next steps? (navigating a document elegantly)
Parameter is not valid calling Bitmap.Save()
WCF URI only works on localhost
Ensuring that the Thread calling Socket.XXXAsync stays alive to complete the IO request (IO Completion Port, C#)
Add Duplicate Row & Columns OnClick c#
How we create pages to show records from a DataTable in a DataGrid in WPF?
SynchronizationContext and InvokeRequired
NHibernate fluent (QueryOver) replacement for HQL with correlated subquery
Designing a translation API - How to handle spaces

Categories

HOME
postgresql
ns2
oracle-sqldeveloper
service
display
microcontroller
windows-server-2008
python-requests
nltk
crash
ebay
travis-ci-cli
yacc
jcodemodel
ndepend
mamp
ng2-charts
watson-dialog
javascript-debugger
opengrok
k-means
draw2d
react-jsx
xna
rvm
v4l2
grunt-contrib-uglify
utc
request-uri
javapns
ocl
schedule
overflow
kubectl
microstrategy
mootools
geocomplete
border-layout
hippocms
client-server
jslider
file-descriptor
collation
peerjs
multi-targeting
rowname
instaparse
laterjs
factorization
abstract
google-finance-api
commercetools
edgar
biginsights
pushwoosh
textblob
tooltwist
xml-simple
glog
targetprocess
gcc-warning
sicstus-prolog
copy-on-write
accpac
qf-test
os.walk
nsurlrequest
selenium-grid2
webshim
farpoint-spread
randomaccessfile
kohana-orm
typography
pyxplot
dd4t
symbian3
select2-rails
pymel
generator-expression
reference-manual
rbm
rfc822
ios6-maps
inserthtml
mcpd
mscorlib
xpolog
qvariant
n2
lwp
hs-err
virtual-destructor
nscharacterset
nstoolbar
table-valued-parameters
appointment
projectgen
cross-cutting-concerns

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile