如何用代码设置RichTextBox的下划线?

Theffff 2014-06-11 11:24:20
wpf的RichTextBox中没有textdecorations这一属性,如何设置?
...全文
143 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Bonjour-你好 2014-06-12
  • 打赏
  • 举报
回复
<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);
}
}
}
}

mayishuo_0226 2014-06-12
  • 打赏
  • 举报
回复
楼上太牛了,学习学习

8,737

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧