Wpf 如何应用多个文本装饰?

Wpf 如何应用多个文本装饰?,wpf,richtextbox,Wpf,Richtextbox,我看到的所有设置下划线、上划线或删除线的示例都是这样的: // setting underline textRange.ApplyPropertyValue(Inline.TextDecorationsProperty, TextDecorations.Underline); // clearing underline textRange.ApplyPropertyValue(Inline.TextDecorationsPropert

我看到的所有设置下划线、上划线或删除线的示例都是这样的:

// setting underline
textRange.ApplyPropertyValue(Inline.TextDecorationsProperty, 
                             TextDecorations.Underline);

// clearing underline
textRange.ApplyPropertyValue(Inline.TextDecorationsProperty, null );
这在我看来过于简单化了;
TextDecorationsProperty
返回装饰的集合-可以同时应用
上划线
下划线
和删除线;像这样设置它们会清除整个集合

这就是我通过
文本装饰位置
切换它们的功能:

var textRange = new TextRange(tb.Selection.Start, tb.Selection.End);
var tdp = textRange.GetPropertyValue(Inline.TextDecorationsProperty);
var textDecorations = tdp.Equals(DependencyProperty.UnsetValue)
                      ? new TextDecorationCollection()
                      : tdp as TextDecorationCollection 
                        ?? new TextDecorationCollection();

var strikethroughs = textDecorations.Where(d => d.Location == TextDecorationLocation.Strikethrough)
                                    .ToList();
if (strikethroughs.Any())
{
   foreach (var strike in strikethroughs)
   {
      textDecorations.Remove(strike);
   }
}
else
{
   textDecorations.Add(TextDecorations.Strikethrough);
}

textRange.ApplyPropertyValue(Inline.TextDecorationsProperty, textDecorations);

这是一个很好的方法,还是我让它变得过于复杂了?

如果您试图在允许装饰组合的同时打开和关闭装饰,您可以执行以下操作:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <StackPanel Grid.Row="0" Orientation="Horizontal">
        <Button Content="Underline" Click="Underline" />
        <Button Content="Strikethrough"  Click="Strikethrough" />
        <Button Content="Baseline" Click="Baseline" />
        <Button Content="Overline"  Click="Overline" />
    </StackPanel>
    <RichTextBox x:Name="tb" Grid.Row="1" />
</Grid>

此代码使用现有的装饰集,然后根据当前装饰是否已附加,将其排除在联合或集合之外-允许打开/关闭装饰。

是否需要纯xaml解决方案?抱歉,我误读了文章,我使用的xaml解决方案一次只允许一个文本装饰。
        private void Underline(object sender, RoutedEventArgs e)
    {
        this.SetDecorations(new TextRange(this.tb.Selection.Start, this.tb.Selection.End), TextDecorations.Underline);
    }

    private void Strikethrough(object sender, RoutedEventArgs e)
    {
        this.SetDecorations(new TextRange(this.tb.Selection.Start, this.tb.Selection.End), TextDecorations.Strikethrough);
    }

    private void Baseline(object sender, RoutedEventArgs e)
    {
        this.SetDecorations(new TextRange(this.tb.Selection.Start, this.tb.Selection.End), TextDecorations.Baseline);
    }

    private void Overline(object sender, RoutedEventArgs e)
    {
        this.SetDecorations(new TextRange(this.tb.Selection.Start, this.tb.Selection.End), TextDecorations.OverLine);
    }

    private void SetDecorations(TextRange textRange, TextDecorationCollection decoration)
    {
        var decorations = textRange.GetPropertyValue(Inline.TextDecorationsProperty) as TextDecorationCollection
                          ?? new TextDecorationCollection();

        decorations = decorations.Contains(decoration.First())
                          ? new TextDecorationCollection(decorations.Except(decoration))
                          : new TextDecorationCollection(decorations.Union(decoration));

        textRange.ApplyPropertyValue(Inline.TextDecorationsProperty, decorations);
    }