wpf的RichTextBox中没有textdecorations这一属性,如何设置?
------解决方案--------------------
<Window x:Class="MyTestWpf.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyTestWpf"
Title="MainWindow" Height="350" Width="525">
<Grid>
<RichTextBox x:Name="richTextBox1" HorizontalAlignment="Left" Height="206" Margin="37,32,0,0" VerticalAlignment="Top" Width="195">
<FlowDocument>
<Paragraph>
<Run Text="RichTextBox"/>
</Paragraph>
</FlowDocument>
</RichTextBox>
<Button Content="Button" HorizontalAlignment="Left" Margin="281,61,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
</Grid>
</Window>
using System;
using System.Linq;
using System.Windows;
using System.Windows.Documents;
namespace MyTestWpf
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var selection = this.richTextBox1.Selection;
if (!selection.IsEmpty) {
var tdc = (TextDecorationCollection)selection.GetPropertyValue(Inline.TextDecorationsProperty);
if (tdc == null
------解决方案--------------------
!tdc.SequenceEqual(TextDecorations.Underline)) {
selection.ApplyPropertyValue(Run.TextDecorationsProperty, TextDecorations.Underline);
} else {
selection.ApplyPropertyValue(Inline.TextDecorationsProperty, null);
}
} else {
var textRange = new TextRange(this.richTextBox1.Document.ContentStart, this.richTextBox1.Document.ContentEnd);
if (!textRange.IsEmpty) {
textRange.ApplyPropertyValue(Inline.TextDecorationsProperty, null);
}
}
}
}