2011-06-25 4 views
0

Je le XAML suivant,Comment lier une chaîne (au format xaml) à la propriété xaml de RichTextBox dans silverlight?

<RichTextBox Name="RichTextBoxPostContent" Margin="0" Padding="8,8,8,0" IsReadOnly="True" Foreground="{x:Null}" Xaml="{Binding Path=PostContent}"/> 

et PostContent (une chaîne) n'a XAML stocké sous forme de chaîne et je ne suis pas sûr de savoir comment le lier à la propriété Xaml de RichTextBox, ce qui suit est la valeur de PostContent,

<Section xml:space="preserve" HasTrailingParagraphBreakOnPaste="False" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"><Paragraph FontSize="11" FontFamily="Portable User Interface" Foreground="#FF000000" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" TextAlignment="Left"><Run Text="aaa" /></Paragraph></Section> 

Répondre

0

Xaml n'est pas une propriété de dépendance dans Silverlight, donc vous ne pouvez pas lier à elle. Vous devrez écrire du code qui s'abonne à INotifyPropertyChanged et fait richTextBox.Xaml = obj.PostContent chaque fois qu'il change.

+0

merci pour la réponse rapide Gabe comment puis-je faire il? (encore merci) –

5

Vous pouvez créer votre propre propriété jointe si vous souhaitez utiliser la liaison de données.

L'exemple de code ci-dessous ajoute une propriété attachée à la zone RichTextBox nommée XamlSource, que vous pouvez utiliser pour lier.

public static class RichTextBoxBinder 
{ 
    #region RichTextBox attached properties 

    public static readonly DependencyProperty XamlSourceProperty = 
    DependencyProperty.RegisterAttached(
     "XamlSource", 
     typeof(string), 
     typeof(RichTextBox), 
     new PropertyMetadata(OnXamlSourcePropertyChanged)); 

    private static void OnXamlSourcePropertyChanged(
    DependencyObject d, 
    DependencyPropertyChangedEventArgs e) 
    { 
    var rtb = d as RichTextBox; 
    if (rtb == null) throw new ArgumentException(
     "Expected a dependency object of type RichTextBox.", "d"); 

    string xaml = null; 
    if (e.NewValue != null) 
    { 
     xaml = e.NewValue as string; 
     if (xaml == null) throw new ArgumentException("Expected a value of type string.", "e.NewValue"); 
    } 

    // Set the xaml and reset selection 
    rtb.Xaml = xaml ?? string.Empty; 
    rtb.Selection.Select(rtb.ContentStart, rtb.ContentStart); 
    } 

    #endregion 

    public static void SetXamlSource(this RichTextBox rtb, string xaml) 
    { 
    rtb.SetValue(XamlSourceProperty, xaml); 
    } 

    public static string GetXamlSource(this RichTextBox rtb) 
    { 
    return (string) rtb.GetValue(XamlSourceProperty); 
    } 
} 

Si la propriété que vous souhaitez lier contre ressemble à ceci:

public string MyRichTextXamlProperty 
{ 
    get 
    { 
    return 
     string.Concat(
     @"<Section xml:space=""preserve"" HasTrailingParagraphBreakOnPaste=""False""", 
     @" xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"">", 
     @"<Paragraph FontSize=""11"" FontFamily=""Portable User Interface""", 
     @" Foreground=""#FF000000"" FontWeight=""Normal"" FontStyle=""Normal""", 
     @" FontStretch=""Normal"" TextAlignment=""Left""><Run Text=""aaa"" />", 
     @"</Paragraph></Section>" 
     ); 
    // Hints: (Thanks Christoph) 
    // 1) Pay special attention that you include the appropriate XML namespaces 
    // e.g. 2nd parameter in string.Concat above. 
    // 2) When you have to use resources, they have to be DynamicResource and 
    // not StaticResource. This is because your resources are only available 
    // at runtime. 
    } 
} 

Ensuite, votre XAML ressemble à ceci:

<UserControl 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:MyNamespace" 
    x:Class="MyClass" > 
    <Grid> 
    <RichTextBox local:RichTextBoxBinder.XamlSource="{Binding MyRichTextXamlProperty}" /> 
    </Grid> 
</UserControl> 
+1

Merci. J'aime cette solution. Juste un indice: vous devez ajouter le '

' avec l'espace de noms XML comme dans la publication. Sinon, vous obtiendrez une 'ArgumentException'. En outre, vous ne pouvez pas utiliser 'StaticResource' etc. http://msdn.microsoft.com/en-us/library/system.windows.controls.richtextbox.xaml(v=vs.95).aspx liste ce qui fonctionne. – Christoph

+0

Merci Christoph. J'ai ajouté vos indices dans l'exemple de code. – base2

Questions connexes