2016-04-14 10 views
-1

私はrichtextboxに奇妙な問題があります。 私たちはWPFリッチテキストボックスの下線

MessageBox(richTextBox.Selection.GetPropertyValue(TextElement.FontStyleProperty).ToString());

を使用し、私はイタリックを取得する場合斜体など

例えば

if (richTextBox.Selection.GetPropertyValue(TextElement.FontStyleProperty).ToString() == "Italic") // Pochylenie 
      { 
       heremycode 
      } 

、テキストが太字であるときを検出します。私は私が考えるメソッド名のみを取得するので、私は

TextBlock.TextDecorationsProperty.ToString()

を使用することはできませんので、私は、取り消し線、下線でexacly同じことをしたいと? "イタリック"のようなものではなく、単に "FontStyleProperty"のようなものです。

private void richTextBox_SelectionChanged(object sender, RoutedEventArgs e) 
    { 
     .... 
     if (richTextBox.Selection.GetPropertyValue(TextElement.FontStyleProperty).ToString() == "Italic" 
     { 
      backgroundP.Stroke = Brushes.Black; 
      backgroundP.Fill = Brushes.LawnGreen; 
      p = true; 
     } 

     TextRange selectionRange = new TextRange(richTextBox.Selection.Start, richTextBox.Selection.End); 

      if (selectionRange.GetPropertyValue(Underline.TextDecorationsProperty).Equals(TextDecorations.Underline)) 
      { 
       MessageBox.Show("Wow We did it :)"); 
       backgroundUnderline.Stroke = Brushes.Black; 
      } 
    } 

とXAMLコード:

<Grid x:Name="Center" Margin="10,231,10,10"> 
      <Rectangle Fill="#B2F4F4F5" Stroke="Black" Margin="1,0,-1,0"/> 
      <Label x:Name="labelNotepad" Content="Notepad" HorizontalAlignment="Left" VerticalAlignment="Top" Width="385" FontWeight="Bold" Background="#FFC1FCFF" FontSize="21.333" Height="56" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Margin="2,1,0,0" BorderThickness="0,0,1,0"/> 
      <RichTextBox x:Name="richTextBox" Margin="1,58,0,0" FontSize="16" BorderThickness="1,2,1,1" BorderBrush="Black" UseLayoutRounding="False" VerticalScrollBarVisibility="Auto" Background="#7FFFFFFF" SelectionChanged="richTextBox_SelectionChanged"> 
       <FlowDocument/> 
      </RichTextBox> 

+0

あなたの試みの結果が何でありますか?例外や空の文字列など? – SeeuD1

+0

メッセージボックスでは、私はTextAligmentPropetyのような一言しか持っていませんが、メソッド名はわかっています。もう一つは「左」、「太字」などの結果があります。 – Sagev

+0

質問とスクリーンショットなどにこれを追加できますか?ヘルプ – SeeuD1

答えて

0

あなたはこの試してみました:これは動作するはずです

TextRange selectionRange = new TextRange(RichTextControl.Selection.Start, RichTextControl.Selection.End); 

     if (selectionRange.GetPropertyValue(Inline.TextDecorationsProperty) == TextDecorations.Underline) 
     { 
     } 

を設定したドントbeacause、あなただけのオブジェクト名を取得しますあなたが文字列として持っていたい依存プロパティ。デコレーションは下線などにすることができます。確認したいものを宣言する必要があります

+0

これは試しても動作しませんが、何をしても問題ありません。(テキストを選択するか、RichTextBoxをクリックしてください)私のmessageBoxはサイレントです:D – Sagev

+0

mhmそれを見て回ることができます – SeeuD1

0

SeeuD1の提案が有効であるため、コードをもっと見る必要があります。ただし、選択範囲全体に下線が引かれている場合にのみ機能します。

選択肢にアンダーラインのテキストがあるかどうかを確認する必要がある場合は、選択肢内のすべてのインラインオブジェクトをチェックする必要があります。

この例では私はあなたのFlowDocument内の段落をチェックします:

 foreach (var block in RichTextBox.Document.Blocks.Where(x => selection.Start.CompareTo(x.ContentEnd) < 0 && selection.End.CompareTo(x.ContentStart) > 0)) 
     { 
      var paragraph = block as Paragraph; 
      if (paragraph != null) 
      { 
       foreach (var selectedInline in paragraph.Inlines.Where(x => selection.Start.CompareTo(x.ContentEnd) < 0 && selection.End.CompareTo(x.ContentStart) > 0)) 
       { 
        if (selectedInline.GetPropertyValue(Inline.TextDecorationsProperty) == TextDecorations.Underline) 
        { 
         MessageBox.Show("Wow We did it :)"); 
        } 
       } 
      } 
     }