简体   繁体   中英

How do i bind string (in xaml format) to xaml property of RichTextBox in silverlight?

I have the following xaml,

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

and PostContent (a string) has xaml stored as string and im not sure how to bind it to RichTextBox's Xaml property, the following is the value of 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>

You can create your own attached property if you want to use data binding.

The code sample below adds an attached property to the RichTextBox named XamlSource, which you can use to bind against.

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);
  }
}

If the property you want to bind against looks like this:

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.
  }
}

Then your xaml looks similar to this:

<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>

Xaml isn't a dependency property in Silverlight, so you can't bind to it. You'll have to write code that subscribes to INotifyPropertyChanged and does richTextBox.Xaml = obj.PostContent whenever it changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM