更新:2007 年 11 月
命名空间:
System.Windows.Controls 程序集:
PresentationFramework(在 PresentationFramework.dll 中)
用于 XAML 的 XMLNS:http://schemas.microsoft.com/winfx/xaml/presentation
<LocalizabilityAttribute(LocalizationCategory.Inherit)> _
<ContentPropertyAttribute("Document")> _
Public Class RichTextBox _
Inherits TextBoxBase _
Implements IAddChild
Dim instance As RichTextBox
[LocalizabilityAttribute(LocalizationCategory.Inherit)]
[ContentPropertyAttribute("Document")]
public class RichTextBox : TextBoxBase,
IAddChild
[LocalizabilityAttribute(LocalizationCategory::Inherit)]
[ContentPropertyAttribute(L"Document")]
public ref class RichTextBox : public TextBoxBase,
IAddChild
/** @attribute LocalizabilityAttribute(LocalizationCategory.Inherit) */
/** @attribute ContentPropertyAttribute("Document") */
public class RichTextBox extends TextBoxBase implements IAddChild
public class RichTextBox extends TextBoxBase implements IAddChild
<RichTextBox>
Document
</RichTextBox>
FlowDocument 是 RichTextBox 支持的唯一子元素。
虽然 RichTextBox 支持复制和粘贴图像,但目前不支持某些特定方案,这些方案可能无法正确运行。 下表总结了这些方案和预期结果。
图像复制源 | 图像粘贴结果 |
|---|
图像源是图形图元文件 | 没有粘贴图像 |
图像源使用相对路径或链接 | 粘贴了乱码或空白图像 |
图像源链接没有以预期图像格式(.PNG、.JPG、.GIF)结尾 | 粘贴了乱码或空白图像 |
从格式不正确的 RichText (RTF) 复制的图像源 | 粘贴了图像源的链接(而非图像) |
将 HTML 内容粘贴到 RichTextBox 中可能导致意外行为,因为 RichTextBox 使用 RTF 格式,而不是直接使用 HTML 格式。
文本在 RichTextBox 中总是换行。如果不希望文本换行,请将 FlowDocument 上的 PageWidth 设置为大于 RichTextBox 的宽度。但是,一旦达到该页宽,文本仍会换行。
可通过 HorizontalContentAlignment 和 VerticalContentAlignment 属性在水平和垂直方向上对齐 RichTextBox 中的内容。可通过 HorizontalAlignment 和 VerticalAlignment 属性对齐页面布局中的 RichTextBox。
默认情况下,滚动条在 RichTextBox 上不可见。若要使滚动条可见,请将 VerticalScrollBarVisibility 属性设置为 Visible 或 Auto。
通常,当 TextBox 或 RichTextBox 中的文本更改时,应使用 TextChanged 事件进行检测,而不是使用您可能认为的 KeyDown。有关示例,请参见如何:检测 TextBox 中的文本何时更改。
RichTextBox 支持多种键盘命令。有关键盘命令的列表,请参见 EditingCommands。
下面的示例声明一个包含简单 FlowDocument 的 RichTextBox。
<RichTextBox Name="richTB">
<FlowDocument>
<Paragraph>
<Run>Paragraph 1</Run>
</Paragraph>
<Paragraph>
<Run>Paragraph 2</Run>
</Paragraph>
<Paragraph>
<Run>Paragraph 3</Run>
</Paragraph>
</FlowDocument>
</RichTextBox>
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Documents;
namespace SDKSample
{
public partial class RichTextBoxExample : Page
{
public RichTextBoxExample()
{
StackPanel myStackPanel = new StackPanel();
// Create a FlowDocument to contain content for the RichTextBox.
FlowDocument myFlowDoc = new FlowDocument();
// Add paragraphs to the FlowDocument.
myFlowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 1")));
myFlowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 2")));
myFlowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 3")));
RichTextBox myRichTextBox = new RichTextBox();
// Add initial content to the RichTextBox.
myRichTextBox.Document = myFlowDoc;
myStackPanel.Children.Add(myRichTextBox);
this.Content = myStackPanel;
}
}
}
更多代码
System..::.Object
System.Windows.Threading..::.DispatcherObject
System.Windows..::.DependencyObject
System.Windows.Media..::.Visual
System.Windows..::.UIElement
System.Windows..::.FrameworkElement
System.Windows.Controls..::.Control
System.Windows.Controls.Primitives..::.TextBoxBase
System.Windows.Controls..::.RichTextBox
此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。
Windows Vista
.NET Framework 和 .NET Compact Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。
.NET Framework
受以下版本支持:3.5、3.0
参考
其他资源