CustomRichTextBox StrikeThrough TextDecoration [英] CustomRichTextBox StrikeThrough TextDecoration

查看:41
本文介绍了CustomRichTextBox StrikeThrough TextDecoration的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 TextDecorations.Strikethrough 装饰按钮添加到我的自定义 RichTextBox 我正在使用下面的代码添加和删除 TextDecoration问题是我收到一个 InvalidCastException: Unable to cast object of type 'MS.Internal.NamedObject' to type 'System.Windows.TextDecorationCollection'. 当我选择的范围大于一个范围时删除线并点击删除线"按钮.

I want to add a TextDecorations.Strikethrough decoration button to my custom RichTextBox i am using the code bellow for adding and removing the TextDecoration the thing is that i am getting an InvalidCastException: Unable to cast object of type 'MS.Internal.NamedObject' to type 'System.Windows.TextDecorationCollection'. when i am selecting a range greater than the one that is strikedthrough and clicking the "StrikeThrough" button.

我的代码

private void StrikeOutButton_Click(object sender, RoutedEventArgs e)
    {
        TextRange range = new TextRange(this.MyRichTextBox.Selection.Start,
                                      this.MyRichTextBox.Selection.End);

        TextDecorationCollection tdc =
            (TextDecorationCollection)this.MyRichTextBox.
                 Selection.GetPropertyValue(Inline.TextDecorationsProperty);
        /*
        if (tdc == null || !tdc.Equals(TextDecorations.Strikethrough))
        {
            tdc = TextDecorations.Strikethrough;
        }
        else
        {
            tdc = new TextDecorationCollection();
        }
         * */
        if (tdc == null || !tdc.Contains(TextDecorations.Strikethrough[0]))
        {
            tdc = TextDecorations.Strikethrough;
        }
        else
        {
            tdc = new TextDecorationCollection();
        }

        range.ApplyPropertyValue(Inline.TextDecorationsProperty, tdc);
    }

注释掉的代码也不起作用.

the comment out code is also not working.

我本来打算发布 ExceptionDetails 但我认为它很清楚.

I was going to post the ExceptionDetails but i think that it's very clear.

有人可以为我提供解决方法吗?

Can someone provide me a workaround?

推荐答案

问题是,如果您的完整文本未使用删除线修饰,您将获得 DependencyProperty.UnsetValue.

The issue is that you will get DependencyProperty.UnsetValue if not your complete text is either decorated with Strikethrough or not.

因此您可以检查 DependencyProperty.UnsetValue 并在这种情况下应用删除线.

So you may check for DependencyProperty.UnsetValue and just apply Strikethrough in that case.

我做了一个简短的测试,这个解决方案对我有用:

I made a short test and this solutions works for me:

private void StrikeOutButton_Click(object sender, RoutedEventArgs e)
    {
        TextRange textRange = new TextRange(TextBox.Selection.Start, TextBox.Selection.End);
        var currentTextDecoration = textRange.GetPropertyValue(Inline.TextDecorationsProperty);

        TextDecorationCollection newTextDecoration;

        if (currentTextDecoration != DependencyProperty.UnsetValue)
            newTextDecoration = ((TextDecorationCollection)currentTextDecoration == TextDecorations.Strikethrough) ? new TextDecorationCollection() : TextDecorations.Strikethrough;
        else
            newTextDecoration = TextDecorations.Strikethrough;

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

这篇关于CustomRichTextBox StrikeThrough TextDecoration的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆