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

Can't find ExtractToDirectory in c#
wcf + The content type text/html of the response message does not match the content type of the binding (application/soap+xml; charset=utf-8)
Inconsistent loading BitmapImage
search through the string from last and find the second occurrence of a string [closed]
c# Method not working as expected Address book project
Unity C# Instantiate prefab not working correctly
change C# object to type decided at run-time
Dynamic organizational chart in Windows Forms
Binding properties of nested entity
Connecting Python SocketServer with C# Client
In c# is it possible to pass or return operators like types [closed]
Updating the Identity AspNetUser does'nt work
Cast item to be type of received parametar
Is there a way to make this a selected item when defining the ListItem in C#?
How to work with sockets in UWP(Window 10)
ThreadPool or Task.Factory

Categories

HOME
skype-for-business
swift3
salesforce
boxplot
livecharts
salesforce-lightning
opc-ua
grid
browser-cache
uml-designer
ionic-native
obd-ii
minimax
google-app-maker
aws-sdk-cpp
ldap-query
virtualenv
logstash-configuration
swift3.1
aiml
gpo
ajaxcontroltoolkit
polybase
serenity-js
connectiq
mattermost
ng-tags-input
pushbullet
devexpress-mvc
utc
emr
jms-topic
semantic-ui-react
vtd-xml
blockly
slim-lang
zimbra
nscollectionview
google-cloud-powershell
colorbox
wufoo
python-venv
inner-join
spring-webflow-2
jks
react.rb
hdpi
confluent
skype-bots
business-catalyst
scikit-image
jira-agile
django-taggit
camellia
jmh
mod
alchemy
dday
jquery-draggable
nonblocking
dynamics-nav-2013
producer-consumer
winforms-interop
eaaccessory
pagecontrol
tinymce-plugins
skview
service-broker
chrome-mobile
cl.exe
javadb
os.walk
dirname
windows-search
twitter-finagle
sendy
image-preloader
new-operator
object-oriented-database
cvi
pthreads-win32
acrofields
facebook-likebox
ekevent
mencoder
kohana-orm
record-locking
wpd
blotter
database-restore
boost.build
tasklist
openafs
php-gettext
parsec
fileoutputstream
scsf
jqzoom
cpack
quazip
iphone-maps
lts
retrospectiva
scala-designer
expander
memory-consumption

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App