【发布时间】:2011-01-22 14:30:25
【问题描述】:
如何从 xaml 中绑定 RichTextArea 的文本
【问题讨论】:
标签: c# silverlight silverlight-4.0 richtextbox
如何从 xaml 中绑定 RichTextArea 的文本
【问题讨论】:
标签: c# silverlight silverlight-4.0 richtextbox
他们在这里得到了更简单的答案:
Silverlight 4 RichTextBox Bind Data using DataContext 它就像一个魅力。
<RichTextBox>
<Paragraph>
<Run Text="{Binding Path=LineFormatted}" />
</Paragraph>
</RichTextBox>
【讨论】:
<RichTextBox> <FlowDocument> <Paragraph> <Run Text="{Binding Path=MyText}" /> </Paragraph> </FlowDocument> </RichTextBox>
这是我想出的解决方案。我创建了一个自定义 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);
}
}
}
【讨论】:
如果您想将 XAML 控件绑定到内联类型控件中,可以使用 InlineUIContainer 类
<RichTextBlock>
<Paragraph>
<InlineUIContainer>
<TextBlock Text="{Binding Name"} />
</InlineUIContainer>
</Paragraph>
</RichTextBlock>
【讨论】:
没有内置的方法可以做到这一点。您可以创建 Text 附加属性并像讨论的那样绑定到它 here
【讨论】:
这无法完成,您必须手动更新它。文档不是 DependencyProperty。
【讨论】: