如何在Silverlight中将字符串(以xaml格式)绑定到RichTextBox的xaml属性?

| 我有以下xaml,
<RichTextBox Name=\"RichTextBoxPostContent\" Margin=\"0\" Padding=\"8,8,8,0\" IsReadOnly=\"True\" Foreground=\"{x:Null}\" Xaml=\"{Binding Path=PostContent}\"/>
而PostContent(字符串)将xaml存储为字符串,并且我不确定如何将其绑定到RichTextBox的Xaml属性,以下是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>
    
已邀请:
        
Xaml
在Silverlight中不是依赖项属性,因此您无法绑定到它。您将必须编写预订
INotifyPropertyChanged
的代码,并在更改时执行does4ѭ的代码。     
        如果要使用数据绑定,可以创建自己的附加属性。 下面的代码示例将一个附加属性添加到名为XamlSource的RichTextBox中,您可以使用该属性进行绑定。
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);
  }
}
如果要绑定的属性如下所示:
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.
  }
}
然后您的xaml看起来类似于:
<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>
    

要回复问题请先登录注册