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

SMTP Email not sending
How to use controls from derived class in base window class [duplicate]
ASP.NET Web API 2 XML Post request ignore XML namespace attribute
USB HID communication using C# ReadAsync method seems impossible to cancel
Where I can find RemotingServices.CreateTransparentProxy implementation?
Loading Grid using coroutines - wait until finish,
Handling COM DLLs with different signatures in C#
How to refresh HelixToolkit ModelVisual3D content on pressed button?
Winforms showing different values from xml
Cannot cast enum to combobox value
How expensive is using a System.Action?
asp.net mvc azure AAD authentication infinite loop
Change mouse cursor position to focused control
Console app - FTP upload stops execution
Update value Ad attribute to null or empty get errror?
MVVM How to set datacontext when viewmodel uses async

Categories

HOME
pact
max
grizzly
azure-active-directory
primefaces
packet
postsharp
spotfire
spss
lithium
css-selectors
glympse
liquidsoap
schema
zabbix
rmarkdown
osx-yosemite
webpack-dev-server
ios-universal-links
apache-httpclient-4.x
dynamics-crm-2011
tracking
fastreport
logstash-configuration
hql
popup
box2d
template10
aiml
scotty
url-parameters
jq
aspxgridview
android-wear-2.0
core-animation
continuous-fourier
expressionengine
workflow-foundation
checkmarx
mobile-development
overflow
tikz
visual-prolog
oracle-nosql
rtos
ilog
wunderground
redhat-brms
jslider
fisheye
azure-web-roles
aura-framework
pyexcel
test-kitchen
chrome-custom-tabs
hdpi
nao-robot
spring-cloud-bus
monaca
enet
openpgp.js
activesupport
htop
try-catch-finally
pyautogui
huawei
easy-thumbnails
gevent
x12
boost-propertytree
radar-chart
clear
kramdown
newlib
event-tracking
database-project
properties-file
avrcp
vensim
system.diagnostics
avd
genero
veeam
accpac
method-overloading
information-hiding
system.net
visual-c#-express-2010
amzi-prolog
android-relativelayout
webshim
rssi
fastcgi-mono-server
facebook-likebox
lastinsertid
google-oauth-java-client
octokit
xrandr
infinity.js
mysql2
gssapi
motordriver
hypertable
certificate-revocation
filedialog
datejs
jqzoom
parallel-python
auto-compile
zipstream
netduino
text-manipulation
pitch-shifting
dongle
time-management
opengl-to-opengles
user-preferences
httpbrowsercapabilities
high-load

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App