You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WPF RichTextBox文本选中区域样式修改问题

WPF RichTextBox 设置文本装饰(下划线、删除线等)的正确方法

你之前尝试用TextDecoration.PenProperty直接设置文本装饰失败,是因为这个属性是TextDecoration对象自身的依赖属性,并非RichTextBox文本范围可直接应用的格式化属性。正确的做法是通过Inline.TextDecorationsProperty来设置,它接受TextDecorationCollection类型的值,集合中可包含一个或多个文本装饰对象(如下划线、删除线)。

基础示例:给选中文本添加下划线

TextSelection textRange = richTextBoxWpf.Selection;
if (textRange != null && !textRange.IsEmpty)
{
    // 创建下划线装饰
    TextDecoration underlineDecoration = new TextDecoration
    {
        Location = TextDecorationLocation.Underline,
        Pen = new System.Windows.Media.Pen(System.Windows.Media.Brushes.Black, 1),
        // 使用字体推荐的粗细单位,适配不同字号
        PenThicknessUnit = TextDecorationUnit.FontRecommended
    };

    // 将装饰放入集合
    TextDecorationCollection decorationCollection = new TextDecorationCollection();
    decorationCollection.Add(underlineDecoration);

    // 应用到选中文本
    textRange.ApplyPropertyValue(Inline.TextDecorationsProperty, decorationCollection);
}

切换逻辑示例:按钮控制下划线开关

如果需要像Bold按钮那样切换下划线状态,可以先判断选中文本是否已有下划线,再决定添加或移除:

TextSelection textRange = richTextBoxWpf.Selection;
if (textRange == null || textRange.IsEmpty) return;

// 获取当前选中文本的文本装饰集合
var currentDecorations = textRange.GetPropertyValue(Inline.TextDecorationsProperty) as TextDecorationCollection;
bool hasUnderline = currentDecorations?.Any(decoration => decoration.Location == TextDecorationLocation.Underline) ?? false;

if (btUnderline.IsChecked ?? false)
{
    if (!hasUnderline)
    {
        TextDecoration underline = new TextDecoration
        {
            Location = TextDecorationLocation.Underline,
            Pen = new System.Windows.Media.Pen(System.Windows.Media.Brushes.Black, 1),
            PenThicknessUnit = TextDecorationUnit.FontRecommended
        };

        // 克隆现有集合(避免修改共享实例)
        TextDecorationCollection newDecorations = currentDecorations?.Clone() ?? new TextDecorationCollection();
        newDecorations.Add(underline);
        textRange.ApplyPropertyValue(Inline.TextDecorationsProperty, newDecorations);
    }
    btUnderline.IsChecked = true;
}
else
{
    if (currentDecorations != null && hasUnderline)
    {
        // 移除下划线装饰
        TextDecorationCollection newDecorations = new TextDecorationCollection(currentDecorations);
        var underlineToRemove = newDecorations.First(d => d.Location == TextDecorationLocation.Underline);
        newDecorations.Remove(underlineToRemove);

        // 如果集合为空,传null清除所有装饰
        textRange.ApplyPropertyValue(Inline.TextDecorationsProperty, newDecorations.Count > 0 ? newDecorations : null);
    }
    btUnderline.IsChecked = false;
}

其他文本装饰类型

除了下划线,还可以设置删除线、上划线等,只需修改TextDecoration.Location属性:

  • 删除线:TextDecorationLocation.Strikethrough
  • 上划线:TextDecorationLocation.OverLine
  • 基线:TextDecorationLocation.Baseline

内容的提问来源于stack exchange,提问作者Wolfgang Schorge

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 04:50:40