你是否正在寻找关于richtextbox的内容?让我把最简洁的东西奉献给你:

richtextbox
在Silverlight4版本中,richtextbox这个控件算是很受期待的控件了,希望通过这篇文章让你对该控件有所了解。
在Sl中,有TextBox,TextBlock,RichTextBox这3个核心的文本控件,从表面上看,richtextbox看起来和一个普通的TextBox并
没有太多的区别,实际上它提供了诸如格式化文本,段落,超链接,内联图像这些功能,它甚至可以显示任何UIElement,比如DataGrid。
MSDN有关于richtextbox内容模型的一个关系图

richtextbox的内容属性是Blocks,这是一个Paragraph的集合,这些Paragraph可以包含Inline元素派生的元素。 比如Run,
Span,Bold,Italic,Hyperlink,InlineUIContainer,其实从图中你可能会注意到Hyperlink比较特殊,它不能包含其它Inline类型的元素。
下面熟悉richtextbox中一些简单的属性:
添加richtextbox:
="True" x:Name="rbtMyrichtextbox">richtextbox常用的属性包括AcceptReturn和IsReadOnly属性,只有在只读模式下,UI元素才是可用的。
添加元素:
前面已经提到,richtextbox包含了Paragraph集合,所以我们可以轻易添加任何内联元素,那么这里我们看一看在一个Paragraph
中添加多个元素的Code
Paragraph prgParagraph = new Paragraph(); Run rnMyText = new Run(); rnMyText.Text = "This is some example text with a "; prgParagraph.Inlines.Add(rnMyText); Hyperlink lnkSSLink = new Hyperlink(); lnkSSLink.Inlines.Add("link to Silverlight Show"); lnkSSLink.NavigateUri = new Uri(""); prgParagraph.Inlines.Add(lnkSSLink); rbtMyrichtextbox.Blocks.Add(prgParagraph);上面的代码中,添加了一些文本和一个超链接,那么首先要做的是实例化一个Paragraph,在这个Paragraph中,我们添加了文本和
超链接,这些对象被添加到该Paragraph中的Inlines集合中,最后将这个Paragraph添加到了richtextbox中
格式文本对象:
Run元素只能包含非格式化的文本,如果想要对文本进行格式化,那么可以采用变通的方式,即将Run元素包含在内联的格式元素中
Paragraph prgParagraph = new Paragraph(); Bold bldText = new Bold(); bldText.Inlines.Add(new Run() { Text = "This is some example text in bold" }); Italic itlText = new Italic(); itlText.Inlines.Add(new Run() { Text = "This is some example text in italics" }); Underline unText = new Underline(); unText.Inlines.Add(new Run() { Text = "This is some example text, underlined" }); Bold bldTextWithItalic = new Bold(); bldTextWithItalic.Inlines.Add(new Italic() { Inlines = { new Run(){ Text = "This is some example text, bold and italic" } } }); prgParagraph.Inlines.Add(bldText); prgParagraph.Inlines.Add(new LineBreak()); prgParagraph.Inlines.Add(itlText); prgParagraph.Inlines.Add(new LineBreak()); prgParagraph.Inlines.Add(unText); prgParagraph.Inlines.Add(new LineBreak()); prgParagraph.Inlines.Add(bldTextWithItalic); rbtMyrichtextbox.Blocks.Add(prgParagraph);上面的代码分别实现了文本的加粗,倾斜,下滑线功能,和添加元素的代码没有太大区别
添加InlineUIContainer:
顾名思义,这个元素就是UI元素的容器,当我们想要在内容中添加Image,DataGrid这些元素的时候,它非常的方便,步骤也是和其它内联元素一样的
先创建一个InlineUIContainer,然后在容器中添加UIElement,把这个容器添加到Paragraph中
InlineUIContainer iuicContainer = new InlineUIContainer(); DataGrid dtgGrid = new DataGrid(); dtgGrid.AutoGenerateColumns = true; dtgGrid.Width = 400; dtgGrid.Height = 200; dtgGrid.ItemsSource = ... iuicContainer.Child = dtgGrid; Paragraph prgParagraph = new Paragraph(); prgParagraph.Inlines.Add(iuicContainer); rbtMyrichtextbox.Blocks.Add(prgParagraph);上面的CODE如果改成XAML声明:
="True" Margin="5" x:Name="rbtMyrichtextbox"> =通过上面的内容已经对richtextbox有了基本的了解,其实之前我们都是通过编程的方式加入这些元素,但是更为常见的场景是
用户可以通过选择某些文本,并对选中的文本进行加粗等操作,richtextbox提供了一个类型为TextSelection的Selection属性,
它包含了当前被选择的文本,然后我们可以通过GetPropertyValue和ApplyPropertyValue方法对选择的文本进行操作
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/shenmilingyu/article-8612-1.html
海南岛边12海里
那些去抢苹果手机的脑残该醒醒了
多170艘还好呢