【问题标题】:Bind the text of RichTextBox from Xaml从 Xaml 绑定 RichTextBox 的文本
【发布时间】:2011-01-22 14:30:25
【问题描述】:

如何从 xaml 中绑定 RichTextArea 的文本

【问题讨论】:

    标签: c# silverlight silverlight-4.0 richtextbox


    【解决方案1】:

    他们在这里得到了更简单的答案:

    Silverlight 4 RichTextBox Bind Data using DataContext 它就像一个魅力。

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

    【讨论】:

    • 这在 Windows 8/8.1 XAML 中也很有效。谢谢!!
    • 简单而有效的解决方案,非常棒
    • 正如@dex3703 所说,这适用于WPF,语法为:&lt;RichTextBox&gt; &lt;FlowDocument&gt; &lt;Paragraph&gt; &lt;Run Text="{Binding Path=MyText}" /&gt; &lt;/Paragraph&gt; &lt;/FlowDocument&gt; &lt;/RichTextBox&gt;
    • 仅供参考。如果用户执行 SelectAll 和 Delete 操作将失败,因为该段落元素将被删除。
    【解决方案2】:

    这是我想出的解决方案。我创建了一个自定义 RichTextViewer 类并继承自 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);
    
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      如果您想将 XAML 控件绑定到内联类型控件中,可以使用 InlineUIContainer

      <RichTextBlock>
      <Paragraph>
          <InlineUIContainer>
              <TextBlock Text="{Binding Name"} />
          </InlineUIContainer>
      </Paragraph>
      </RichTextBlock>
      

      【讨论】:

        【解决方案4】:

        没有内置的方法可以做到这一点。您可以创建 Text 附加属性并像讨论的那样绑定到它 here

        【讨论】:

        • 提供的链接特定于 WPF。 Silverlight 中的 RichTextBox 上没有 Document 属性。
        • 我想支持史蒂夫的评论。
        【解决方案5】:

        这无法完成,您必须手动更新它。文档不是 DependencyProperty。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-01-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-22
          • 2012-04-30
          相关资源
          最近更新 更多