C# 从 Xaml 绑定 RichTextBox 的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2361219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bind the text of RichTextBox from Xaml
提问by user281947
How to Bind the text of RichTextArea from xaml
如何从xaml绑定RichTextArea的文本
采纳答案by Arsen Mkrtchyan
回答by Ana Betts
This can't be done, you have to manually update it. Document is not a DependencyProperty.
这是无法完成的,您必须手动更新它。文档不是 DependencyProperty。
回答by Todd Main
Should be able to happen in SL4 RC. See What is the best substitute for FlowDocument in Silverlight?
应该能够在 SL4 RC 中发生。请参阅Silverlight 中 FlowDocument 的最佳替代品是什么?
回答by e-rock
Here is the solution I came up with. I created a custom RichTextViewer class and inherited from 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);
}
}
}
回答by m1m1k
They've got the easier answer here:
他们在这里得到了更简单的答案:
Silverlight 4 RichTextBox Bind Data using DataContextand it works like a charm.
Silverlight 4 RichTextBox 使用 DataContext 绑定数据,它就像一个魅力。
<RichTextBox>
<Paragraph>
<Run Text="{Binding Path=LineFormatted}" />
</Paragraph>
</RichTextBox>
回答by Kevin Tan
You can use the InlineUIContainer
class if you want to bind a XAML control inside of an inline typed control
InlineUIContainer
如果要在内联类型控件内绑定 XAML 控件,可以使用该类
<RichTextBlock>
<Paragraph>
<InlineUIContainer>
<TextBlock Text="{Binding Name"} />
</InlineUIContainer>
</Paragraph>
</RichTextBlock>