2010-03-02 4 views

Répondre

3

Il n'y a pas construit de façon de le faire. Vous pouvez créer une propriété attachée au texte et lier comme décrit here

+3

Le lien fourni est spécifique à WPF. Le RichTextBox dans Silverlight n'a pas de propriété Document sur celui-ci. –

+0

J'aimerais appuyer le commentaire de Steve. –

0

Ceci ne peut pas être fait, vous devez le mettre à jour manuellement. Le document n'est pas un DependencyProperty.

3

Voici la solution que j'ai trouvée. J'ai créé une classe RichTextViewer personnalisée et j'ai hérité 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

Ils ont la réponse facile ici:

Silverlight 4 RichTextBox Bind Data using DataContext et il fonctionne comme un charme.

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

Cela fonctionne également très bien dans Windows 8/8.1 XAML. Merci!! – dex3703

+1

solution simple mais efficace, brillant – DNKROZ

+4

Comme @ dex3703 disais, cela fonctionne dans WPF avec la syntaxe: ' ' – maxp

1

Vous pouvez utiliser la classe InlineUIContainer si vous voulez lier un contrôle XAML à l'intérieur d'une ligne typée contrôle

Questions connexes