从Xaml绑定RichTextBox的文本

发布于 2024-08-23 09:17:23 字数 31 浏览 5 评论 0原文

如何从xaml绑定RichTextArea的文本

How to Bind the text of RichTextArea from xaml

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

2024-08-30 09:17:23

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

Silverlight 4 RichTextBox 使用 DataContext 绑定数据
它就像一个魅力。

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

They've got the easier answer here:

Silverlight 4 RichTextBox Bind Data using DataContext
and it works like a charm.

<RichTextBox>
  <Paragraph>
    <Run Text="{Binding Path=LineFormatted}" />
  </Paragraph>
</RichTextBox>
2024-08-30 09:17:23

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

        }
    }
}

Here is the solution I came up with. I created a custom RichTextViewer class and inherited from 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);

        }
    }
}
墨落画卷 2024-08-30 09:17:23

您可以使用 InlineUIContainer 类(如果要在内联类型控件内部绑定 XAML 控件)

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

You can use the InlineUIContainer class if you want to bind a XAML control inside of an inline typed control

<RichTextBlock>
<Paragraph>
    <InlineUIContainer>
        <TextBlock Text="{Binding Name"} />
    </InlineUIContainer>
</Paragraph>
</RichTextBlock>
绮筵 2024-08-30 09:17:23

没有内置的方法可以做到这一点。您可以创建文本附加属性并绑定到它,就像讨论的 此处

There is no built in way to do that. You can create Text attached property and bind to it like discussed here

江心雾 2024-08-30 09:17:23

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

This can't be done, you have to manually update it. Document is not a DependencyProperty.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文