【问题标题】:wpf richtextbox: error on "decor == TextDecorations.Strikethrough"wpf richtextbox:“decor == TextDecorations.Strikethrough”上的错误
【发布时间】:2022-08-14 09:48:41
【问题描述】:

试图检测 RichTextbox 中的选定文本是下划线还是删除线。在函数调用上出现语法错误。不知道如何解决。

然而,我让它适用于粗体和斜体的情况。似乎 TextDecorations 在 .NET 库中不存在与粗体和斜体案例类似的方式?

<Window x:Class=\"WpfApp1.MainWindow\"
        xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"
        xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\"
        xmlns:d=\"http://schemas.microsoft.com/expression/blend/2008\"
        xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\"
        xmlns:local=\"clr-namespace:WpfApp1\"
        mc:Ignorable=\"d\"
        Title=\"MainWindow\" Height=\"450\" Width=\"800\">
<Grid>
   <RichTextBox Name=\"RichTextBox1\"/>
</Grid>
</Window>
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
namespace WpfApp1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private bool SelectionIsStrikethrough()
        {
            var decor = RichTextBox1.Selection.GetPropertyValue(TextDecorationsProperty);
            var unset = DependencyProperty.UnsetValue;
            var SelectionIsStrike = decor != unset && (TextDecorations)decor == TextDecorations.Strikethrough;
            return SelectionIsStrike;
        }

        private bool SelectionIsUnderline()
        {
            var decor = RichTextBox1.Selection.GetPropertyValue(TextDecorationsProperty);
            var unset = DependencyProperty.UnsetValue;
            var SelectionIsStrike = decor != unset && (TextDecorations)decor == TextDecorations.Underline;
            return SelectionIsStrike;
        }
        
        private bool SelectionIsBold()
        {
            var weight = RichTextBox1.Selection.GetPropertyValue(FontWeightProperty);
            var unset = DependencyProperty.UnsetValue;
            var SelectionIsBold = weight != unset && (FontWeight)weight == FontWeights.Bold;
            return SelectionIsBold;
        }
        private bool SelectionIsItalic()
        {
            var style = RichTextBox1.Selection.GetPropertyValue(FontStyleProperty);
            var unset = DependencyProperty.UnsetValue;
            var SelectionIsItalic = style != unset && (FontStyle)style == FontStyles.Italic;
            return SelectionIsItalic;
        }
    }       
}

    标签: c# wpf


    【解决方案1】:
    private bool SelectionIsUnderline()
    {
    var decor = mEdit.Selection.GetPropertyValue(Inline.TextDecorationsProperty);
    var unset = DependencyProperty.UnsetValue;
    var SelectionIsStrike = decor != unset && decor == TextDecorations.Underline;
    return SelectionIsStrike;
    }
    
    private bool SelectionIsStrikethrough()
    {
    var decor = mEdit.Selection.GetPropertyValue(Inline.TextDecorationsProperty);
    var unset = DependencyProperty.UnsetValue;
    var SelectionIsStrike = decor != unset && decor == TextDecorations.Strikethrough;
    return SelectionIsStrike;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-21
      • 2011-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-25
      • 2010-10-15
      相关资源
      最近更新 更多