2016-10-17 3 views
0

したがって、長いテキスト行にバインドされるWPF RichTextBoxがあります。RichTextBox内のテキストの部分的な文字列を動的に設定する

オブジェクトの2つのセットを使用して、2つのポインタ間のテキストにスタイルが適用されるようにします。 (たとえば、テキストの背景色/前景色を変更します)。テキストが2つのポインタの間になくなると、スタイルは元のスタイルにリセットされなければなりません。

希望する動作は、クリックしてドラッグしてウェブサイト上のテキストをハイライト/選択する方法と似ています(ただし同じではありません)。クリックしてドラッグするのではなく、ユーザーがこれを行うことはできません。エンドポイントをプログラムで決定します。

私はそれを行う方法を理解できないようです。私は<Run></Run>に必要なスタイルを適用できますが、コントロール内からテキストの特定の部分文字列を取得し、プログラムでRunタグを適用(削除)する方法を理解できません。

理想的な解決策は、selectメソッドによって適用されるスタイルを変更することです。私は(マウスを無効にすることなく)ユーザからの選択を無効にすることが可能であり、それでも私が利用できるプログラム選択が可能であるかどうかわからないので、これを行うのを少し気にしています。

+0

は画像と説明しよう。テキストの選択を移動することは既にRTBで利用可能です。 – AnjumSKhan

答えて

1

更新:私は元々、RichTextBoxではなく、TextBlockについて話していたと思います。ソリューションに絶対にRichTextBoxが必要な場合は、どこかで使用可能なRTFパーサーを見つける必要があります。

あなたができることの1つは、RTFまたはHTMLコントロールを使用することです。

または、次のコードを使用して、私が銃で私の頭に書いたものを使用することができます(実際には、私ができるかどうかを確認するために書きました)。間違いなくMVVMに対する罪ですが、目を閉じて<Bold>などのタグはXAMLではなく任意のマークアップ言語であるとふりまとめることができます。

とにかく:フォーマットする希望の範囲が変更されたら、FormattedTextプロパティを更新し、PropertyChangedを上げます。

C#

namespace HollowEarth.AttachedProperties 
{ 
    public static class TextProperties 
    { 
     #region TextProperties.XAMLText Attached Property 
     public static String GetXAMLText(TextBlock obj) 
     { 
      return (String)obj.GetValue(XAMLTextProperty); 
     } 

     public static void SetXAMLText(TextBlock obj, String value) 
     { 
      obj.SetValue(XAMLTextProperty, value); 
     } 

     /// <summary> 
     /// Convert raw string into formatted text in a TextBlock: 
     /// 
     /// @"This <Bold>is a test <Italic>of the</Italic></Bold> text." 
     /// 
     /// Text will be parsed as XAML TextBlock content. 
     /// 
     /// See WPF TextBlock documentation for full formatting. It supports spans and all kinds of things. 
     /// 
     /// </summary> 
     public static readonly DependencyProperty XAMLTextProperty = 
      DependencyProperty.RegisterAttached("XAMLText", typeof(String), typeof(TextProperties), 
               new PropertyMetadata("", XAMLText_PropertyChanged)); 

     // I don't recall why this was necessary; maybe it wasn't. 
     public static Stream GetStream(String s) 
     { 
      MemoryStream stream = new MemoryStream(); 
      StreamWriter writer = new StreamWriter(stream); 

      writer.Write(s); 
      writer.Flush(); 
      stream.Position = 0; 

      return stream; 
     } 

     private static void XAMLText_PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
     { 
      if (d is TextBlock) 
      { 
       var ctl = d as TextBlock; 

       try 
       { 
        // XAML needs a containing tag with a default namespace. We're parsing 
        // TextBlock content, so make the parent a TextBlock to keep the schema happy. 
        // TODO: If you want any content not in the default schema, you're out of luck. 
        var strText = String.Format(@"<TextBlock xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"">{0}</TextBlock>", e.NewValue); 

        TextBlock parsedContent = System.Windows.Markup.XamlReader.Load(GetStream(strText)) as TextBlock; 

        // The Inlines collection contains the structured XAML content of a TextBlock 
        ctl.Inlines.Clear(); 

        // UI elements are removed from the source collection when the new parent 
        // acquires them, so pass in a copy of the collection to iterate over. 
        ctl.Inlines.AddRange(parsedContent.Inlines.ToList()); 
       } 
       catch (Exception ex) 
       { 
        System.Diagnostics.Trace.WriteLine(String.Format("Error in HollowEarth.AttachedProperties.TextProperties.XAMLText_PropertyChanged: {0}", ex.Message)); 
        throw; 
       } 
      } 
     } 
     #endregion TextProperties.XAMLText Attached Property 
    } 
} 

その他のC#

​​

XAML

<TextBlock 
     xmlns:heap="clr-namespace:HollowEarth.AttachedProperties" 
     heap:TextProperties.XAMLText="{Binding FormattedText}" 
     /> 

    <TextBlock 
     xmlns:heap="clr-namespace:HollowEarth.AttachedProperties" 
     heap:TextProperties.XAMLText="This is &lt;Italic Foreground=&quot;Red&quot;&gt;italic and &lt;Bold&gt;bolded&lt;/Bold&gt;&lt;/Italic&gt; text" 
     /> 
+0

ええ、私は本質的に 'TextBlock'を' RichTextBox'に変換する方法を尋ねていたことに気付きました。私はそれに応じて正気を尊重するように編集しました。 – Airhead

+1

@Airheadサニティは過大評価されています。私の答えは 'TextBlock'でそれを行います。 –

関連する問題