簡體   English   中英

如何檢查RichTextBox中的文本是否帶有下划線?

[英]How to check if text in a RichTextBox is underlined?

我目前正在使用WPF編寫一個文本編輯器,其中包含一個工具欄和一個RichTextBox。 工具欄中的每個按鈕定義了將應用於所選文本的文本屬性(使用costum屬性)。 工作正常。

問題

如果用戶單擊文本上的某個位置,並且其文本屬性應用於當前位置,則按鈕應突出顯示。 當我嘗試檢查當前選擇是否帶有下划線/刪除線時,它不起作用。

private void checkToolBarButtons()
    {
        // this method checks if the button should be highlighted or not.
        Func<Button, object> check = (Button button) =>
        {
            try
            {
                // the button has two costum properties that allow to determine which text property should be
                // applied to the selected text.
                string propString = ToolBarButton.GetDocumentProperty(button);
                // e.g. "TextDecorations"
                DependencyProperty dependency = this.getPropertyByString(propString);
                // e.g. TextDecorationsProperty
                string propertyValue = ToolBarButton.GetDocumentPropertyValue(button);
                // e.g. "Underline"

                if (dependency != null)
                {
                    TextRange selectionRange = new TextRange(this.richTextBox.Selection.Start, this.richTextBox.Selection.End);
                    object selectedProperty = selectionRange.GetPropertyValue(dependency);

                    if (selectedProperty.GetType() == typeof(TextDecorationCollection) && ((TextDecorationCollection)selectedProperty).Count > 0)
                    {
                        if (selectedProperty.Equals(TextDecorations.Underline))
                        {
                            // this code is never reached 
                            selectedProperty = "Underline";
                        }
                        else if (selectedProperty.Equals(TextDecorations.Strikethrough))
                        {
                            // this code is never reached 
                            selectedProperty = "Strikethrough";
                        }
                    }

                    if (selectedProperty.ToString() == propertyValue)
                    {
                        button.Background = new SolidColorBrush(Colors.Yellow);
                    }
                    else
                    {
                        button.Background = new SolidColorBrush(Colors.LightGray);
                    }
                }
            }
            catch (Exception ex)
            {

            }
            return null;
        };

        foreach (FrameworkElement ctrl in toolBar.Children)
        {
            if (ctrl.GetType() == typeof(StackPanel))
            {
                foreach (Button button in ((Panel)ctrl).Children)
                {
                    check(button);
                }
            }
            else if (ctrl.GetType() == typeof(Button))
            {
                check((Button)ctrl);
            }
        }
    }

    /// <summary>
    /// converts a string to a DependencyProperty
    /// </summary>
    /// <param name="propertyString"></param>
    /// <returns></returns>
    private DependencyProperty getPropertyByString(string propertyString)
    {
        switch (propertyString)
        {
            case ("FontStyleProperty"): return FontStyleProperty;
            case ("FontWeightProperty"): return FontWeightProperty;
            case ("TextDecorations"): return TextBlock.TextDecorationsProperty;
            case ("TextAlignment"): return TextBlock.TextAlignmentProperty;
            default:
                break;
        }

        return null;
    }

XAML

<UserControl x:Class="EasyControls.TextEditor"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:EasyControls"
         mc:Ignorable="d" Height="236.318" Width="493.571">
<Border BorderThickness="1" BorderBrush="Blue">
    <DockPanel Background="White">
        <WrapPanel x:Name="toolBar" Orientation="Horizontal" Background="#FF2B2B8F" DockPanel.Dock="Top">
            <StackPanel x:Name="textAlignPanel"  Background="#FFF4F4F5" Height="21" Orientation="Horizontal" Margin="2">
                <Button local:ToolBarButton.DocumentProperty="TextAlignment" local:ToolBarButton.DocumentPropertyValue="Left"
                         Margin="2 2 2 2" Click="ToolBarButton_Click" Width="20">
                    <Image Source="img/Left.png" />
                </Button>
                <Button local:ToolBarButton.DocumentProperty="TextAlignment" local:ToolBarButton.DocumentPropertyValue="Center"
                         Margin="2 2 2 2" Click="ToolBarButton_Click" Width="20">
                    <Image Source="img/Center.png" />
                </Button>
                <Button local:ToolBarButton.DocumentProperty="TextAlignment" local:ToolBarButton.DocumentPropertyValue="Right"
                         Margin="2 2 2 2" Click="ToolBarButton_Click" Width="20">
                    <Image Source="img/Right.png" />
                </Button>
                <Button local:ToolBarButton.DocumentProperty="TextAlignment" local:ToolBarButton.DocumentPropertyValue="Justify"
                         Margin="2 2 2 2" Click="ToolBarButton_Click" Width="20">
                    <Image Source="img/Justify.png" />
                </Button>
            </StackPanel>
            <StackPanel x:Name="fontStylePanel"  Background="#FFF4F4F5" Height="21" Orientation="Horizontal">
                <Button local:ToolBarButton.DocumentProperty="FontWeightProperty" local:ToolBarButton.DocumentPropertyValue="Bold"
                         Margin="2 2 2 2" Click="ToolBarButton_Click" FontWeight="Bold" Width="20">
                    <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">b</TextBlock>
                </Button>
                <Button local:ToolBarButton.DocumentProperty="FontStyleProperty" local:ToolBarButton.DocumentPropertyValue="Italic"
                         Margin="1 2 2 2" Click="ToolBarButton_Click" FontStyle="Italic" Width="20">
                    <TextBlock  HorizontalAlignment="Center" VerticalAlignment="Center">i</TextBlock>
                </Button>
                <Button local:ToolBarButton.DocumentProperty="TextDecorations" local:ToolBarButton.DocumentPropertyValue="Underline"
                        Margin="1 2 2 2" Click="ToolBarButton_Click" Width="20" >
                    <TextBlock TextDecorations="Underline"  HorizontalAlignment="Center" VerticalAlignment="Center">u</TextBlock>
                </Button>
                <Button local:ToolBarButton.DocumentProperty="TextDecorations" local:ToolBarButton.DocumentPropertyValue="Strikethrough"
                        Margin="1 2 2 2" Click="ToolBarButton_Click" Width="20" >
                    <TextBlock TextDecorations="Strikethrough" HorizontalAlignment="Center" VerticalAlignment="Center">s</TextBlock>
                </Button>
            </StackPanel>
        </WrapPanel>
        <RichTextBox x:Name="richTextBox" Background="White" SelectionChanged="RichTextBox_SelectionChanged" IsDocumentEnabled="True" DockPanel.Dock="Bottom" VerticalAlignment="Stretch" BorderThickness="0" BorderBrush="{x:Null}">
            <FlowDocument>
                <Paragraph/>
            </FlowDocument>
        </RichTextBox>
    </DockPanel>
</Border>

在此處輸入圖片說明

您首先要檢查selectedProperty是否為TextDecorationCollection ,然后在您期望它為TextDecorations.UnderlineTextDecorations.Strikethrough之后立即TextDecorations.Strikethrough 這是沒有道理的。

您可能想要將selectedProperty TextDecorationCollection轉換為TextDecorationCollection ,然后對其進行迭代。 像這樣:

...
TextRange selectionRange = new TextRange(this.richTextBox.Selection.Start, this.richTextBox.Selection.End);
object selectedProperty = selectionRange.GetPropertyValue(TextBlock.TextDecorationsProperty);

TextDecorationCollection textDecorationCollection = selectedProperty as TextDecorationCollection;
if (textDecorationCollection != null)
{
    foreach (TextDecoration textDecoration in textDecorationCollection)
    {
        if (textDecoration.Location == TextDecorationLocation.Underline)
        {
            // this code is never reached 
            selectedProperty = "Underline";
        }
        else if (textDecoration.Location == TextDecorationLocation.Strikethrough)
        {
            // this code is never reached 
            selectedProperty = "Strikethrough";
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM