2010-03-02 26 views

Respuesta

3

No hay construida en forma de hacerlo. Puede crear propiedad adjunta de texto y vincularla como se describe here

+3

El enlace que es específico para WPF. RichTextBox en Silverlight no tiene una propiedad de documento en él. –

+0

Me gustaría hacer un segundo comentario de Steve. –

0

Esto no se puede hacer, tiene que actualizarlo manualmente. El documento no es una DependencyProperty.

3

Aquí está la solución que se me ocurrió. Creé una clase RichTextViewer personalizada y heredó de 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); 

     } 
    } 
} 
24

que tienen la respuesta más fácil aquí:

Silverlight 4 RichTextBox Bind Data using DataContext y funciona como un encanto.

<RichTextBox> 
    <Paragraph> 
    <Run Text="{Binding Path=LineFormatted}" /> 
    </Paragraph> 
</RichTextBox> 
+2

Esto también funciona muy bien en Windows 8/8.1 XAML. ¡¡Gracias!! – dex3703

+1

solución simple pero eficaz, brillante – DNKROZ

+4

Como @ dex3703 decía, esto funciona en WPF con la sintaxis: ' ' – maxp

1

Puede utilizar la clase InlineUIContainer si desea enlazar un control XAML en el interior de una línea de control mecanografiadas