2011-06-27 62 views
0

我需要將格式化文本數據綁定到RichTextBox。對於格式化,我似乎必須創建一系列具有特定格式的運行,然後將它們添加到段落中,並將其添加到RichTextBox上的blocks屬性中。我試圖綁定一個段落屬性塊,但它似乎不允許。段落沒有項目源將其綁定到運行列表。我怎樣才能將運行列表數據綁定到RichTextBox控件?數據綁定格式化文本到Silverlight中的richtextbox 4

謝謝

回答

0

這是我想出的解決方案。我創建了一個自定義的RichTextViewer類並從RichTextBox繼承。

using System.Windows.Documents; 
using System.Windows.Markup; 
using System.Windows.Media; 

namespace System.Windows.Controls 
{ 
    public class RichTextViewer : RichTextBox 
    { 
     public const string RichTextPropertyName = "RichText"; 

     public static readonly DependencyProperty RichTextProperty = 
      DependencyProperty.Register(RichTextPropertyName, 
             typeof (string), 
             typeof (RichTextBox), 
             new PropertyMetadata(
              new PropertyChangedCallback 
               (RichTextPropertyChanged))); 

     public RichTextViewer() 
     { 
      IsReadOnly = true; 
      Background = new SolidColorBrush {Opacity = 0}; 
      BorderThickness = new Thickness(0); 
     } 

     public string RichText 
     { 
      get { return (string) GetValue(RichTextProperty); } 
      set { SetValue(RichTextProperty, value); } 
     } 

     private static void RichTextPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs) 
     { 
      ((RichTextBox) dependencyObject).Blocks.Add(
       XamlReader.Load((string) dependencyPropertyChangedEventArgs.NewValue) as Paragraph); 

     } 
    } 
}