C# 从Xaml绑定RichTextBox的文本
如何从xaml绑定RichTextArea的文本没有内置的方法。您可以创建文本附加属性并像前面讨论的那样绑定到它这无法完成,您必须手动更新它。文档不是从属属性。应该能够在SL4 RC中发生。请参见这是我提出的解决方案。我创建了一个自定义RichTextViewer类,并从RichTextBox继承C# 从Xaml绑定RichTextBox的文本,c#,silverlight,silverlight-4.0,richtextbox,C#,Silverlight,Silverlight 4.0,Richtextbox,如何从xaml绑定RichTextArea的文本没有内置的方法。您可以创建文本附加属性并像前面讨论的那样绑定到它这无法完成,您必须手动更新它。文档不是从属属性。应该能够在SL4 RC中发生。请参见这是我提出的解决方案。我创建了一个自定义RichTextViewer类,并从RichTextBox继承 using System.Windows.Documents; using System.Windows.Markup; using System.Windows.Media; namespace
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);
}
}
}
他们在这里得到了更简单的答案: 它就像一个符咒
<RichTextBox>
<Paragraph>
<Run Text="{Binding Path=LineFormatted}" />
</Paragraph>
</RichTextBox>
如果要在内联类型控件中绑定XAML控件,可以使用该类
<RichTextBlock>
<Paragraph>
<InlineUIContainer>
<TextBlock Text="{Binding Name"} />
</InlineUIContainer>
</Paragraph>
</RichTextBlock>
提供的链接特定于WPF。Silverlight中的RichTextBox没有文档属性。我想支持Steve的评论。这在Windows 8/8.1 XAML中也非常有效。谢谢BrilliantAs@dex3703说,简单但有效的解决方案,这在WPF中使用语法:
仅供他人参考。。如果用户执行SelectAll和Delete操作,则此操作将失败,因为该段落元素将被删除。