[源码下载]
作者: webabcd
介绍
Silverlight 4.0 控件一览:
在线DEMO
http://www.cnblogs.com/webabcd/archive/2010/08/09/1795417.html
示例
1、ViewBox 的 Demo
ViewBoxDemo.xaml
ViewBoxDemo.xaml.cs
2、RichTextBox 的 Demo
RichTextBoxDemo.xaml
RichTextBoxDemo.xaml.cs
OK
[源码下载]
作者: webabcd
介绍
Silverlight 4.0 控件一览:
在线DEMO
http://www.cnblogs.com/webabcd/archive/2010/08/09/1795417.html
示例
1、ViewBox 的 Demo
ViewBoxDemo.xaml
代码
<
navigation:Page
x:Class
="Silverlight40.Control.ViewBoxDemo"
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d ="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:navigation ="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
Title ="ViewBoxDemo Page" >
< Grid x:Name ="LayoutRoot" Background ="White" >
< StackPanel Background ="AntiqueWhite" HorizontalAlignment ="Left" >
<!-- 用于演示 ViewBox.Stretch 属性 -->
< StackPanel Margin ="5" Width ="200" >
< TextBlock Text ="Stretch" />
< Button Name ="btn1" Click ="stretchNone" Content ="None" />
< Button Name ="btn2" Click ="stretchFill" Content ="Fill" />
< Button Name ="btn3" Click ="stretchUniform" Content ="Uniform" />
< Button Name ="btn4" Click ="stretchUniformToFill" Content ="UniformToFill" />
</ StackPanel >
<!-- 用于演示 ViewBox.StretchDirection 属性 -->
< StackPanel Margin ="5" Width ="200" >
< TextBlock Text ="StretchDirection" />
< Button Name ="btn5" Click ="stretchDirectionUpOnly" Content ="UpOnly" />
< Button Name ="btn6" Click ="stretchDirectionDownOnly" Content ="DownOnly" />
< Button Name ="btn7" Click ="stretchDirectionBoth" Content ="Both" />
</ StackPanel >
<!-- 用于演示 ViewBox.HorizontalAlignment 属性 -->
< StackPanel Margin ="5" Width ="200" >
< TextBlock Text ="HorizontalAlignment" />
< Button Name ="btn8" Click ="horizontalAlignmentCenter" Content ="Center" />
< Button Name ="btn9" Click ="horizontalAlignmentLeft" Content ="Left" />
< Button Name ="btn10" Click ="horizontalAlignmentRight" Content ="Right" />
< Button Name ="btn11" Click ="horizontalAlignmentStretch" Content ="Stretch" />
</ StackPanel >
<!-- 用于演示 ViewBox.VerticalAlignment 属性 -->
< StackPanel Margin ="5" Width ="200" >
< TextBlock Text ="VerticalAlignment" />
< Button Name ="btn12" Click ="verticalAlignmentCenter" Content ="Center" />
< Button Name ="btn13" Click ="verticalAlignmentTop" Content ="Top" />
< Button Name ="btn14" Click ="verticalAlignmentBottom" Content ="Bottom" />
< Button Name ="btn15" Click ="verticalAlignmentStretch" Content ="Stretch" />
</ StackPanel >
<!-- 用于显示当前 ViewBox 的 Stretch 值,StretchDirection 值,HorizontalAlignment 值,VerticalAlignment 值 -->
< StackPanel Margin ="5" >
< TextBlock Name ="lblMsg" />
</ StackPanel >
<!-- 用于演示 ViewBox 的各种效果 -->
< StackPanel Width ="500" Height ="300" Background ="Black" >
< ViewBox Name ="viewBox" Width ="500" Height ="300" >
<!-- 注:ViewBox 内只能有一个子元素 -->
< Image Source ="/Resource/logo.jpg" />
</ ViewBox >
</ StackPanel >
</ StackPanel >
</ Grid >
</ navigation:Page >
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d ="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:navigation ="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
Title ="ViewBoxDemo Page" >
< Grid x:Name ="LayoutRoot" Background ="White" >
< StackPanel Background ="AntiqueWhite" HorizontalAlignment ="Left" >
<!-- 用于演示 ViewBox.Stretch 属性 -->
< StackPanel Margin ="5" Width ="200" >
< TextBlock Text ="Stretch" />
< Button Name ="btn1" Click ="stretchNone" Content ="None" />
< Button Name ="btn2" Click ="stretchFill" Content ="Fill" />
< Button Name ="btn3" Click ="stretchUniform" Content ="Uniform" />
< Button Name ="btn4" Click ="stretchUniformToFill" Content ="UniformToFill" />
</ StackPanel >
<!-- 用于演示 ViewBox.StretchDirection 属性 -->
< StackPanel Margin ="5" Width ="200" >
< TextBlock Text ="StretchDirection" />
< Button Name ="btn5" Click ="stretchDirectionUpOnly" Content ="UpOnly" />
< Button Name ="btn6" Click ="stretchDirectionDownOnly" Content ="DownOnly" />
< Button Name ="btn7" Click ="stretchDirectionBoth" Content ="Both" />
</ StackPanel >
<!-- 用于演示 ViewBox.HorizontalAlignment 属性 -->
< StackPanel Margin ="5" Width ="200" >
< TextBlock Text ="HorizontalAlignment" />
< Button Name ="btn8" Click ="horizontalAlignmentCenter" Content ="Center" />
< Button Name ="btn9" Click ="horizontalAlignmentLeft" Content ="Left" />
< Button Name ="btn10" Click ="horizontalAlignmentRight" Content ="Right" />
< Button Name ="btn11" Click ="horizontalAlignmentStretch" Content ="Stretch" />
</ StackPanel >
<!-- 用于演示 ViewBox.VerticalAlignment 属性 -->
< StackPanel Margin ="5" Width ="200" >
< TextBlock Text ="VerticalAlignment" />
< Button Name ="btn12" Click ="verticalAlignmentCenter" Content ="Center" />
< Button Name ="btn13" Click ="verticalAlignmentTop" Content ="Top" />
< Button Name ="btn14" Click ="verticalAlignmentBottom" Content ="Bottom" />
< Button Name ="btn15" Click ="verticalAlignmentStretch" Content ="Stretch" />
</ StackPanel >
<!-- 用于显示当前 ViewBox 的 Stretch 值,StretchDirection 值,HorizontalAlignment 值,VerticalAlignment 值 -->
< StackPanel Margin ="5" >
< TextBlock Name ="lblMsg" />
</ StackPanel >
<!-- 用于演示 ViewBox 的各种效果 -->
< StackPanel Width ="500" Height ="300" Background ="Black" >
< ViewBox Name ="viewBox" Width ="500" Height ="300" >
<!-- 注:ViewBox 内只能有一个子元素 -->
< Image Source ="/Resource/logo.jpg" />
</ ViewBox >
</ StackPanel >
</ StackPanel >
</ Grid >
</ navigation:Page >
ViewBoxDemo.xaml.cs
代码
/*
* ViewBox - 一个容器控件,其内只能有一个子元素。ViewBox 可以决定其内的子元素如何拉伸、缩放、对齐
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
namespace Silverlight40.Control
{
public partial class ViewBoxDemo : Page
{
public ViewBoxDemo()
{
InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
ShowResult();
}
// 用于显示当前 ViewBox 的 Stretch 值,StretchDirection 值,HorizontalAlignment 值,VerticalAlignment 值
private void ShowResult()
{
lblMsg.Text = string .Format( " Stretch: {0}, StretchDirection: {1}, HorizontalAlignment: {2}, VerticalAlignment: {3} " ,
viewBox.Stretch.ToString(),
viewBox.StretchDirection.ToString(),
viewBox.HorizontalAlignment.ToString(),
viewBox.VerticalAlignment.ToString());
}
/*
* ViewBox.Stretch - 子元素在 ViewBox 内的拉伸模式 [System.Windows.Media.Stretch 枚举]
* Stretch.None - 不做处理。不做任何拉伸处理,填充内容保持原始大小
* Stretch.Fill - 充满。调整填充内容,以充满整个容器,填充内容比例变为容器比例
* Stretch.Uniform - 等比适应。调整填充内容,以适合容器尺寸,填充内容会做等比例调整(默认值)
* 如果填充内容与容器比例不一样,那么填充内容调整的结果为:
* 使得填充内容的宽与容器的宽相等,或者 填充内容的高与容器的高相等。填充内容会被完整显示
* Stretch.UniformToFill - 等比充满。调整填充内容,以适合容器尺寸,填充内容会做等比例调整
* 如果填充内容与容器比例不一样,那么填充内容调整的结果为:
* 使得填充内容的宽与容器的宽相等,并且 填充内容的高与容器的高相等。填充内容会被做相应的剪裁
*/
private void stretchNone( object sender, RoutedEventArgs e)
{
viewBox.Stretch = Stretch.None;
ShowResult();
}
private void stretchFill( object sender, RoutedEventArgs e)
{
viewBox.Stretch = Stretch.Fill;
ShowResult();
}
private void stretchUniform( object sender, RoutedEventArgs e)
{
viewBox.Stretch = Stretch.Uniform;
ShowResult();
}
private void stretchUniformToFill( object sender, RoutedEventArgs e)
{
viewBox.Stretch = Stretch.UniformToFill;
ShowResult();
}
/*
* ViewBox.StretchDirection - 子元素在 ViewBox 内的缩放模式 [System.Windows.Controls.StretchDirection 枚举]
* StretchDirection.UpOnly - 当子元素小于 ViewBox 时,子元素会放大
* StretchDirection.DownOnly - 当子元素大于 ViewBox 时,子元素会缩小
* StretchDirection.Both - 不做任何处理(默认值)
*/
private void stretchDirectionUpOnly( object sender, RoutedEventArgs e)
{
viewBox.StretchDirection = StretchDirection.UpOnly;
ShowResult();
}
private void stretchDirectionDownOnly( object sender, RoutedEventArgs e)
{
viewBox.StretchDirection = StretchDirection.DownOnly;
ShowResult();
}
private void stretchDirectionBoth( object sender, RoutedEventArgs e)
{
viewBox.StretchDirection = StretchDirection.Both;
ShowResult();
}
/*
* ViewBox.HorizontalAlignment - 子元素在 ViewBox 内的水平方向的对齐模式 [System.Windows.HorizontalAlignment 枚举]
* Center, Left, Right, Stretch(默认值)
*/
private void horizontalAlignmentCenter( object sender, RoutedEventArgs e)
{
viewBox.HorizontalAlignment = HorizontalAlignment.Center;
ShowResult();
}
private void horizontalAlignmentLeft( object sender, RoutedEventArgs e)
{
viewBox.HorizontalAlignment = HorizontalAlignment.Left;
ShowResult();
}
private void horizontalAlignmentRight( object sender, RoutedEventArgs e)
{
viewBox.HorizontalAlignment = HorizontalAlignment.Right;
ShowResult();
}
private void horizontalAlignmentStretch( object sender, RoutedEventArgs e)
{
viewBox.HorizontalAlignment = HorizontalAlignment.Stretch;
ShowResult();
}
/*
* ViewBox.VerticalAlignment - 子元素在 ViewBox 内的垂直方向的对齐模式 [System.Windows.VerticalAlignment 枚举]
* Center, Top, Bottom, Stretch(默认值)
*/
private void verticalAlignmentCenter( object sender, RoutedEventArgs e)
{
viewBox.VerticalAlignment = VerticalAlignment.Center;
ShowResult();
}
private void verticalAlignmentTop( object sender, RoutedEventArgs e)
{
viewBox.VerticalAlignment = VerticalAlignment.Top;
ShowResult();
}
private void verticalAlignmentBottom( object sender, RoutedEventArgs e)
{
viewBox.VerticalAlignment = VerticalAlignment.Bottom;
ShowResult();
}
private void verticalAlignmentStretch( object sender, RoutedEventArgs e)
{
viewBox.VerticalAlignment = VerticalAlignment.Stretch;
ShowResult();
}
}
}
* ViewBox - 一个容器控件,其内只能有一个子元素。ViewBox 可以决定其内的子元素如何拉伸、缩放、对齐
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
namespace Silverlight40.Control
{
public partial class ViewBoxDemo : Page
{
public ViewBoxDemo()
{
InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
ShowResult();
}
// 用于显示当前 ViewBox 的 Stretch 值,StretchDirection 值,HorizontalAlignment 值,VerticalAlignment 值
private void ShowResult()
{
lblMsg.Text = string .Format( " Stretch: {0}, StretchDirection: {1}, HorizontalAlignment: {2}, VerticalAlignment: {3} " ,
viewBox.Stretch.ToString(),
viewBox.StretchDirection.ToString(),
viewBox.HorizontalAlignment.ToString(),
viewBox.VerticalAlignment.ToString());
}
/*
* ViewBox.Stretch - 子元素在 ViewBox 内的拉伸模式 [System.Windows.Media.Stretch 枚举]
* Stretch.None - 不做处理。不做任何拉伸处理,填充内容保持原始大小
* Stretch.Fill - 充满。调整填充内容,以充满整个容器,填充内容比例变为容器比例
* Stretch.Uniform - 等比适应。调整填充内容,以适合容器尺寸,填充内容会做等比例调整(默认值)
* 如果填充内容与容器比例不一样,那么填充内容调整的结果为:
* 使得填充内容的宽与容器的宽相等,或者 填充内容的高与容器的高相等。填充内容会被完整显示
* Stretch.UniformToFill - 等比充满。调整填充内容,以适合容器尺寸,填充内容会做等比例调整
* 如果填充内容与容器比例不一样,那么填充内容调整的结果为:
* 使得填充内容的宽与容器的宽相等,并且 填充内容的高与容器的高相等。填充内容会被做相应的剪裁
*/
private void stretchNone( object sender, RoutedEventArgs e)
{
viewBox.Stretch = Stretch.None;
ShowResult();
}
private void stretchFill( object sender, RoutedEventArgs e)
{
viewBox.Stretch = Stretch.Fill;
ShowResult();
}
private void stretchUniform( object sender, RoutedEventArgs e)
{
viewBox.Stretch = Stretch.Uniform;
ShowResult();
}
private void stretchUniformToFill( object sender, RoutedEventArgs e)
{
viewBox.Stretch = Stretch.UniformToFill;
ShowResult();
}
/*
* ViewBox.StretchDirection - 子元素在 ViewBox 内的缩放模式 [System.Windows.Controls.StretchDirection 枚举]
* StretchDirection.UpOnly - 当子元素小于 ViewBox 时,子元素会放大
* StretchDirection.DownOnly - 当子元素大于 ViewBox 时,子元素会缩小
* StretchDirection.Both - 不做任何处理(默认值)
*/
private void stretchDirectionUpOnly( object sender, RoutedEventArgs e)
{
viewBox.StretchDirection = StretchDirection.UpOnly;
ShowResult();
}
private void stretchDirectionDownOnly( object sender, RoutedEventArgs e)
{
viewBox.StretchDirection = StretchDirection.DownOnly;
ShowResult();
}
private void stretchDirectionBoth( object sender, RoutedEventArgs e)
{
viewBox.StretchDirection = StretchDirection.Both;
ShowResult();
}
/*
* ViewBox.HorizontalAlignment - 子元素在 ViewBox 内的水平方向的对齐模式 [System.Windows.HorizontalAlignment 枚举]
* Center, Left, Right, Stretch(默认值)
*/
private void horizontalAlignmentCenter( object sender, RoutedEventArgs e)
{
viewBox.HorizontalAlignment = HorizontalAlignment.Center;
ShowResult();
}
private void horizontalAlignmentLeft( object sender, RoutedEventArgs e)
{
viewBox.HorizontalAlignment = HorizontalAlignment.Left;
ShowResult();
}
private void horizontalAlignmentRight( object sender, RoutedEventArgs e)
{
viewBox.HorizontalAlignment = HorizontalAlignment.Right;
ShowResult();
}
private void horizontalAlignmentStretch( object sender, RoutedEventArgs e)
{
viewBox.HorizontalAlignment = HorizontalAlignment.Stretch;
ShowResult();
}
/*
* ViewBox.VerticalAlignment - 子元素在 ViewBox 内的垂直方向的对齐模式 [System.Windows.VerticalAlignment 枚举]
* Center, Top, Bottom, Stretch(默认值)
*/
private void verticalAlignmentCenter( object sender, RoutedEventArgs e)
{
viewBox.VerticalAlignment = VerticalAlignment.Center;
ShowResult();
}
private void verticalAlignmentTop( object sender, RoutedEventArgs e)
{
viewBox.VerticalAlignment = VerticalAlignment.Top;
ShowResult();
}
private void verticalAlignmentBottom( object sender, RoutedEventArgs e)
{
viewBox.VerticalAlignment = VerticalAlignment.Bottom;
ShowResult();
}
private void verticalAlignmentStretch( object sender, RoutedEventArgs e)
{
viewBox.VerticalAlignment = VerticalAlignment.Stretch;
ShowResult();
}
}
}
2、RichTextBox 的 Demo
RichTextBoxDemo.xaml
代码
<
navigation:Page
x:Class
="Silverlight40.Control.RichTextBoxDemo"
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d ="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:navigation ="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
Title ="RichTextBoxDemo Page" >
< StackPanel HorizontalAlignment ="Left" >
< StackPanel Background ="AntiqueWhite" Orientation ="Horizontal" >
< ComboBox x:Name ="cmbFontSize" FontSize =" {Binding SelectedItem.FontSize, RelativeSource={RelativeSource Self}} " SelectionChanged ="cmbFontSize_SelectionChanged" >
< ComboBoxItem Content ="16" Tag ="16" FontSize ="16" IsSelected ="True" />
< ComboBoxItem Content ="48" Tag ="48" FontSize ="48" />
</ ComboBox >
< ComboBox x:Name ="cmbFontFamily" FontFamily =" {Binding SelectedItem.FontFamily, RelativeSource={RelativeSource Self}} " SelectionChanged ="cmbFontFamily_SelectionChanged" >
< ComboBoxItem Content ="Arial" Tag ="Arial" FontFamily ="Arial" IsSelected ="True" />
< ComboBoxItem Content ="Verdana" Tag ="Verdana" FontFamily ="Verdana" />
</ ComboBox >
< Button Name ="btnBold" Content ="加粗" Click ="btnBold_Click" />
< Button Name ="btnItalic" Content ="斜体" Click ="btnItalic_Click" />
< Button Name ="btnUnderline" Content ="下划线" Click ="btnUnderline_Click" />
< Button Name ="btnHyperlink" Content ="插入超级链接的 Demo" Click ="btnHyperlink_Click" />
< Button Name ="btnUIElement" Content ="插入 UIElement 的 Demo" Click ="btnUIElement_Click" />
< ToggleButton Name ="btnXaml" Content ="xaml" Checked ="btnXaml_Checked" Unchecked ="btnXaml_Checked" />
< ToggleButton Name ="btnPreview" Content ="预览" Checked ="btnPreview_Checked" Unchecked ="btnPreview_Checked" />
< Button Name ="btnTextPointerDemo" Content ="TextPointer 的 Demo (将光标当前所在位置到文本结尾的文字全变为绿色)" Click ="btnTextPointerDemo_Click" />
</ StackPanel >
< Grid Width ="600" Height ="400" >
< RichTextBox Name ="richTextBox" VerticalScrollBarVisibility ="Auto" >
< Paragraph >
webabcd
</ Paragraph >
< Paragraph >
< Span > webabcd </ Span >
< LineBreak />
< Bold > webabcd </ Bold >
< LineBreak />
< Italic > webabcd </ Italic >
< LineBreak />
< Underline > webabcd </ Underline >
< LineBreak />
< Run FontFamily ="Arial" FontSize ="20" FontStretch ="normal" FontStyle ="Italic" FontWeight ="Bold" Foreground ="Red" Textdecorations ="underline" Text ="webabcd" />
< LineBreak />
< Hyperlink TargetName ="_black" NavigateUri ="http://webabcd.cnblogs.com/" > webabcd </ Hyperlink >
< LineBreak />
< InlineUIContainer >
< Image Source ="/Resource/logo.jpg" Height ="100" Width ="100" ToolTipService.ToolTip ="我是提示" />
</ InlineUIContainer >
</ Paragraph >
</ RichTextBox >
< TextBox Name ="txtXaml" textwrapping ="Wrap" VerticalScrollBarVisibility ="Auto" Visibility ="Collapsed" />
</ Grid >
< TextBlock Name ="lblMsg" />
</ StackPanel >
</ navigation:Page >
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d ="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:navigation ="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
Title ="RichTextBoxDemo Page" >
< StackPanel HorizontalAlignment ="Left" >
< StackPanel Background ="AntiqueWhite" Orientation ="Horizontal" >
< ComboBox x:Name ="cmbFontSize" FontSize =" {Binding SelectedItem.FontSize, RelativeSource={RelativeSource Self}} " SelectionChanged ="cmbFontSize_SelectionChanged" >
< ComboBoxItem Content ="16" Tag ="16" FontSize ="16" IsSelected ="True" />
< ComboBoxItem Content ="48" Tag ="48" FontSize ="48" />
</ ComboBox >
< ComboBox x:Name ="cmbFontFamily" FontFamily =" {Binding SelectedItem.FontFamily, RelativeSource={RelativeSource Self}} " SelectionChanged ="cmbFontFamily_SelectionChanged" >
< ComboBoxItem Content ="Arial" Tag ="Arial" FontFamily ="Arial" IsSelected ="True" />
< ComboBoxItem Content ="Verdana" Tag ="Verdana" FontFamily ="Verdana" />
</ ComboBox >
< Button Name ="btnBold" Content ="加粗" Click ="btnBold_Click" />
< Button Name ="btnItalic" Content ="斜体" Click ="btnItalic_Click" />
< Button Name ="btnUnderline" Content ="下划线" Click ="btnUnderline_Click" />
< Button Name ="btnHyperlink" Content ="插入超级链接的 Demo" Click ="btnHyperlink_Click" />
< Button Name ="btnUIElement" Content ="插入 UIElement 的 Demo" Click ="btnUIElement_Click" />
< ToggleButton Name ="btnXaml" Content ="xaml" Checked ="btnXaml_Checked" Unchecked ="btnXaml_Checked" />
< ToggleButton Name ="btnPreview" Content ="预览" Checked ="btnPreview_Checked" Unchecked ="btnPreview_Checked" />
< Button Name ="btnTextPointerDemo" Content ="TextPointer 的 Demo (将光标当前所在位置到文本结尾的文字全变为绿色)" Click ="btnTextPointerDemo_Click" />
</ StackPanel >
< Grid Width ="600" Height ="400" >
< RichTextBox Name ="richTextBox" VerticalScrollBarVisibility ="Auto" >
< Paragraph >
webabcd
</ Paragraph >
< Paragraph >
< Span > webabcd </ Span >
< LineBreak />
< Bold > webabcd </ Bold >
< LineBreak />
< Italic > webabcd </ Italic >
< LineBreak />
< Underline > webabcd </ Underline >
< LineBreak />
< Run FontFamily ="Arial" FontSize ="20" FontStretch ="normal" FontStyle ="Italic" FontWeight ="Bold" Foreground ="Red" Textdecorations ="underline" Text ="webabcd" />
< LineBreak />
< Hyperlink TargetName ="_black" NavigateUri ="http://webabcd.cnblogs.com/" > webabcd </ Hyperlink >
< LineBreak />
< InlineUIContainer >
< Image Source ="/Resource/logo.jpg" Height ="100" Width ="100" ToolTipService.ToolTip ="我是提示" />
</ InlineUIContainer >
</ Paragraph >
</ RichTextBox >
< TextBox Name ="txtXaml" textwrapping ="Wrap" VerticalScrollBarVisibility ="Auto" Visibility ="Collapsed" />
</ Grid >
< TextBlock Name ="lblMsg" />
</ StackPanel >
</ navigation:Page >
RichTextBoxDemo.xaml.cs
代码
/*
* RichTextBox - 编辑器。用于显示或编辑文本、超链、图片、UI元素等
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using System.Windows.Media.Imaging;
namespace Silverlight40.Control
{
public partial class RichTextBoxDemo : Page
{
public RichTextBoxDemo()
{
InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
/*
* RichTextBox 的内容结构说明如下:
*
* Block - 抽象类。块级内容的基类
* Paragraph - 段落,包含一组内联元素。继承自 Block
* Inline - 抽象类。内联元素的基类
* Run - 文本。继承自 Inline
* LineBreak - 换行。继承自 Inline
* Span - 文本。继承自 Inline
* Hyperlink - 超链接。继承自 Span
* Bold - 加粗。继承自 Span
* Italic - 斜体。继承自 Span
* Underline - 下划线。继承自 Span
* InlineUIContainer - 用于承载 UIElement 类型的对象。继承自 Span
* TextElement - TextElement是 Inline 的基类,可以对其设置如下属性
* FontSize - 字体大小
* FontFamily - 字体名称
* Foreground - 字体颜色(因为 xaml 属性仅支持可以表示为一个字符串的属性值。所以这里只能使用 SolidColorBrush,而不能使用 LinearGradientBrush 之类的)
* FontWeight - 笔画粗细
* FontStyle - 是否斜体
* FontStretch - 字体的拉伸程度(需要字体支持)
*/
/*
* Paragraph.Inlines - 段落所包含的内联元素的集合
* RichTextBox.Blocks - RichTextBox 中的块级元素的集合
* RichTextBox.Xaml - RichTextBox 中的内容的 xaml 代码
*/
Bold bold = new Bold();
bold.Inlines.Add( " web " );
LineBreak lineBreak = new LineBreak();
Run run = new Run();
run.Text = " abcd " ;
Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add(bold);
paragraph.Inlines.Add(lineBreak);
paragraph.Inlines.Add(run);
richTextBox.Blocks.Add(paragraph);
}
/*
* RichTextBox.Selection - 获取 RichTextBox 中的选中内容,返回一个 TextSelection 类型的对象
* TextSelection.ApplyPropertyValue(DependencyProperty formattingProperty, Object value) - 为选中的内容指定其格式的属性和值
* DependencyProperty formattingProperty - 格式的属性
* Object value - 格式的属性的值
* TextSelection.Text - 选中的内容中的纯文本内容
* TextSelection.Xaml - 选中的内容的 xaml 代码
* TextSelection.Insert() - 把当前选中的内容替换成指定的 TextElement
* 以下分别举例如何设置选中文本的字体大小、字体名称、加粗、斜体、下划线
*/
private void cmbFontSize_SelectionChanged( object sender, SelectionChangedEventArgs e)
{
if (richTextBox != null && richTextBox.Selection.Text.Length > 0 )
{
richTextBox.Selection.ApplyPropertyValue(Run.FontSizeProperty, double .Parse((cmbFontSize.SelectedItem as ComboBoxItem).Tag.ToString()));
}
}
private void cmbFontFamily_SelectionChanged( object sender, SelectionChangedEventArgs e)
{
if (richTextBox != null && richTextBox.Selection.Text.Length > 0 )
{
richTextBox.Selection.ApplyPropertyValue(Run.FontFamilyProperty, new FontFamily((cmbFontFamily.SelectedItem as ComboBoxItem).Tag.ToString()));
}
}
private void btnBold_Click( object sender, RoutedEventArgs e)
{
if (richTextBox.Selection.Text.Length > 0 )
{
if (richTextBox.Selection.GetPropertyValue(Run.FontWeightProperty) is FontWeight && ((FontWeight)richTextBox.Selection.GetPropertyValue(Run.FontWeightProperty)) == FontWeights.normal)
richTextBox.Selection.ApplyPropertyValue(Run.FontWeightProperty, FontWeights.Bold);
else
richTextBox.Selection.ApplyPropertyValue(Run.FontWeightProperty, FontWeights.normal);
}
}
private void btnItalic_Click( object sender, RoutedEventArgs e)
{
if (richTextBox.Selection.Text.Length > 0 )
{
if (richTextBox.Selection.GetPropertyValue(Run.FontStyleProperty) is FontStyle && ((FontStyle)richTextBox.Selection.GetPropertyValue(Run.FontStyleProperty)) == FontStyles.normal)
richTextBox.Selection.ApplyPropertyValue(Run.FontStyleProperty, FontStyles.Italic);
else
richTextBox.Selection.ApplyPropertyValue(Run.FontStyleProperty, FontStyles.normal);
}
}
private void btnUnderline_Click( object sender, RoutedEventArgs e)
{
if (richTextBox.Selection.Text.Length > 0 )
{
if (richTextBox.Selection.GetPropertyValue(Run.TextdecorationsProperty) == null )
richTextBox.Selection.ApplyPropertyValue(Run.TextdecorationsProperty, Textdecorations.Underline);
else
richTextBox.Selection.ApplyPropertyValue(Run.TextdecorationsProperty, null );
}
}
/*
* TextSelection.Insert() - 把当前选中的内容替换成指定的 TextElement
* Hyperlink - 一个用于显示超链接的内联元素。Hyperlink继承自Span,Span继承自Inline,Inline继承自TextElement
* 以下举例如何插入超链接(要在 RichTextBox 使超链接有效,需要将 RichTextBox 设置为显示状态)
*/
private void btnHyperlink_Click( object sender, RoutedEventArgs e)
{
Hyperlink hyperlink = new Hyperlink();
hyperlink.TargetName = " _blank " ;
hyperlink.NavigateUri = new Uri( " http://webabcd.cnblogs.com/ " );
hyperlink.Inlines.Add( " webabcd blog " );
richTextBox.Selection.Insert(hyperlink);
}
/*
* InlineUIContainer - 一个容器,可以承载任何 UIElement 类型的对象。InlineUIContainer继承自Inline
* 以下举例如何插入图片
*/
private void btnUIElement_Click( object sender, RoutedEventArgs e)
{
Image img = new Image();
img.source = new BitmapImage( new Uri( " /Resource/logo.jpg " , UriKind.Relative));
img.Width = 100 ;
img.Height = 100 ;
InlineUIContainer container = new InlineUIContainer();
container.Child = img;
richTextBox.Selection.Insert(container);
}
/*
* RichTextBox.Xaml - RichTextBox 中的内容的 xaml 代码
* 以下举例如何在“显示模式”和“代码模式”之间切换
*/
private void btnXaml_Checked( object sender, RoutedEventArgs e)
{
if (btnXaml.IsChecked.Value)
{
txtXaml.Text = richTextBox.Xaml;
txtXaml.Visibility = Visibility.Visible;
}
else
{
richTextBox.Xaml = txtXaml.Text;
txtXaml.Visibility = Visibility.Collapsed;
}
}
/*
* RichTextBox.IsReadOnly - 指定是否可以在 RichTextBox 中编辑内容
* true - 显示模式
* false - 编辑模式(此模式下可以通过快捷键的方式来支持撤销 Ctrl+Z 和重做 Ctrl+Y 操作)
* 以下举例如何预览 RichTextBox 中的内容(注:只有在显示模式中超链接才生效)
*/
private void btnPreview_Checked( object sender, RoutedEventArgs e)
{
richTextBox.IsReadOnly = btnPreview.IsChecked.Value;
}
private void btnTextPointerDemo_Click( object sender, RoutedEventArgs e)
{
/*
* TextPointer - 表示 RichTextBox 中的一个位置
* 插入位置(Insertion Position)的概念:
* TextPointer 出现在内容中的字符之间则为插入位置
* TextPointer 出现在定义内容结构的元素标记之间,则此位置不是插入位置。例如,两个相邻段落标记(即前一个段落的结束标记与下一个段落的开始标记)之间的位置就是一个有效的 TextPointer 位置,但不是插入位置
* TextPointer.Compareto(TextPointer tp) - 当前 TextPointer 位于指定的 TextPointer 之前则为 -1,之后则为 1,相同则为 0
* TextPointer.IsAtInsertionPosition - 当前 TextPointer 是否是一个插入位置
* TextPointer.GetNextInsertionPosition(LogicalDirection direction) - 获取当前 TextPointer 的按指定的逻辑方向的下一个插入位置
* LogicalDirection - 逻辑方向 [System.Windows.Documents.LogicalDirection 枚举]
* LogicalDirection.Forward - 向前
* LogicalDirection.Backward - 向后
* TextPointer.GetPositionAtOffset(int offset, LogicalDirection direction) - 获取当前 TextPointer 的按指定的逻辑方向偏移了指定值的 TextPointer
* TextPointer.Parent - 包含当前 TextPointer 的容器。比如,文字的 Parent 是 run ;RichTextBox.ContentEnd 的 Parent 是 RichTextBox
*
* RichTextBox.ContentStart - RichTextBox 内容的开头的 TextPointer(肯定不是插入位置)
* RichTextBox.ContentEnd - RichTextBox 内容的结尾的 TextPointer(肯定不是插入位置)
* RichTextBox.GetPositionFromPoint(Point point) - 获取离指定 Point 最近的插入位置
* RichTextBox.Selection - 获取 RichTextBox 中的选中内容,返回一个 TextSelection 类型的对象
* TextSelection.Start - 选中内容的开头 TextPointer
* TextSelection.End - 选中内容的开头 TextPointer
* TextSelection.Select(TextPointer tp1, TextPointer tp2) - 指定两个 TextPointer 来更新选中内容
*
* 在 RichTextBox 中被视为一个符号的有:TextElement 的开始或结束标记;InlineUIContainer 中的 UIElement 元素;Run 中的字符
*/
// 将光标当前所在位置到文本结尾的文字全变为绿色
TextPointer currentPointer = richTextBox.Selection.Start;
TextPointer endPointer = richTextBox.ContentEnd.GetNextInsertionPosition(LogicalDirection.Backward);
richTextBox.Selection.Select(currentPointer, endPointer);
richTextBox.Selection.ApplyPropertyValue(Run.ForegroundProperty, Colors.Green);
// 将光标当前所在位置到文本结尾的文字信息输出到页面上
while (currentPointer.Compareto(endPointer) == - 1 )
{
string currentChar = "" ;
TextPointer nextPointer = currentPointer.GetNextInsertionPosition(LogicalDirection.Forward);
if (nextPointer != null )
{
richTextBox.Selection.Select(currentPointer, nextPointer);
if (richTextBox.Selection.Text.Length != 0 )
currentChar = richTextBox.Selection.Text;
}
lblMsg.Text += currentChar;
currentPointer = currentPointer.GetNextInsertionPosition(LogicalDirection.Forward);
}
}
}
}
* RichTextBox - 编辑器。用于显示或编辑文本、超链、图片、UI元素等
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using System.Windows.Media.Imaging;
namespace Silverlight40.Control
{
public partial class RichTextBoxDemo : Page
{
public RichTextBoxDemo()
{
InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
/*
* RichTextBox 的内容结构说明如下:
*
* Block - 抽象类。块级内容的基类
* Paragraph - 段落,包含一组内联元素。继承自 Block
* Inline - 抽象类。内联元素的基类
* Run - 文本。继承自 Inline
* LineBreak - 换行。继承自 Inline
* Span - 文本。继承自 Inline
* Hyperlink - 超链接。继承自 Span
* Bold - 加粗。继承自 Span
* Italic - 斜体。继承自 Span
* Underline - 下划线。继承自 Span
* InlineUIContainer - 用于承载 UIElement 类型的对象。继承自 Span
* TextElement - TextElement是 Inline 的基类,可以对其设置如下属性
* FontSize - 字体大小
* FontFamily - 字体名称
* Foreground - 字体颜色(因为 xaml 属性仅支持可以表示为一个字符串的属性值。所以这里只能使用 SolidColorBrush,而不能使用 LinearGradientBrush 之类的)
* FontWeight - 笔画粗细
* FontStyle - 是否斜体
* FontStretch - 字体的拉伸程度(需要字体支持)
*/
/*
* Paragraph.Inlines - 段落所包含的内联元素的集合
* RichTextBox.Blocks - RichTextBox 中的块级元素的集合
* RichTextBox.Xaml - RichTextBox 中的内容的 xaml 代码
*/
Bold bold = new Bold();
bold.Inlines.Add( " web " );
LineBreak lineBreak = new LineBreak();
Run run = new Run();
run.Text = " abcd " ;
Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add(bold);
paragraph.Inlines.Add(lineBreak);
paragraph.Inlines.Add(run);
richTextBox.Blocks.Add(paragraph);
}
/*
* RichTextBox.Selection - 获取 RichTextBox 中的选中内容,返回一个 TextSelection 类型的对象
* TextSelection.ApplyPropertyValue(DependencyProperty formattingProperty, Object value) - 为选中的内容指定其格式的属性和值
* DependencyProperty formattingProperty - 格式的属性
* Object value - 格式的属性的值
* TextSelection.Text - 选中的内容中的纯文本内容
* TextSelection.Xaml - 选中的内容的 xaml 代码
* TextSelection.Insert() - 把当前选中的内容替换成指定的 TextElement
* 以下分别举例如何设置选中文本的字体大小、字体名称、加粗、斜体、下划线
*/
private void cmbFontSize_SelectionChanged( object sender, SelectionChangedEventArgs e)
{
if (richTextBox != null && richTextBox.Selection.Text.Length > 0 )
{
richTextBox.Selection.ApplyPropertyValue(Run.FontSizeProperty, double .Parse((cmbFontSize.SelectedItem as ComboBoxItem).Tag.ToString()));
}
}
private void cmbFontFamily_SelectionChanged( object sender, SelectionChangedEventArgs e)
{
if (richTextBox != null && richTextBox.Selection.Text.Length > 0 )
{
richTextBox.Selection.ApplyPropertyValue(Run.FontFamilyProperty, new FontFamily((cmbFontFamily.SelectedItem as ComboBoxItem).Tag.ToString()));
}
}
private void btnBold_Click( object sender, RoutedEventArgs e)
{
if (richTextBox.Selection.Text.Length > 0 )
{
if (richTextBox.Selection.GetPropertyValue(Run.FontWeightProperty) is FontWeight && ((FontWeight)richTextBox.Selection.GetPropertyValue(Run.FontWeightProperty)) == FontWeights.normal)
richTextBox.Selection.ApplyPropertyValue(Run.FontWeightProperty, FontWeights.Bold);
else
richTextBox.Selection.ApplyPropertyValue(Run.FontWeightProperty, FontWeights.normal);
}
}
private void btnItalic_Click( object sender, RoutedEventArgs e)
{
if (richTextBox.Selection.Text.Length > 0 )
{
if (richTextBox.Selection.GetPropertyValue(Run.FontStyleProperty) is FontStyle && ((FontStyle)richTextBox.Selection.GetPropertyValue(Run.FontStyleProperty)) == FontStyles.normal)
richTextBox.Selection.ApplyPropertyValue(Run.FontStyleProperty, FontStyles.Italic);
else
richTextBox.Selection.ApplyPropertyValue(Run.FontStyleProperty, FontStyles.normal);
}
}
private void btnUnderline_Click( object sender, RoutedEventArgs e)
{
if (richTextBox.Selection.Text.Length > 0 )
{
if (richTextBox.Selection.GetPropertyValue(Run.TextdecorationsProperty) == null )
richTextBox.Selection.ApplyPropertyValue(Run.TextdecorationsProperty, Textdecorations.Underline);
else
richTextBox.Selection.ApplyPropertyValue(Run.TextdecorationsProperty, null );
}
}
/*
* TextSelection.Insert() - 把当前选中的内容替换成指定的 TextElement
* Hyperlink - 一个用于显示超链接的内联元素。Hyperlink继承自Span,Span继承自Inline,Inline继承自TextElement
* 以下举例如何插入超链接(要在 RichTextBox 使超链接有效,需要将 RichTextBox 设置为显示状态)
*/
private void btnHyperlink_Click( object sender, RoutedEventArgs e)
{
Hyperlink hyperlink = new Hyperlink();
hyperlink.TargetName = " _blank " ;
hyperlink.NavigateUri = new Uri( " http://webabcd.cnblogs.com/ " );
hyperlink.Inlines.Add( " webabcd blog " );
richTextBox.Selection.Insert(hyperlink);
}
/*
* InlineUIContainer - 一个容器,可以承载任何 UIElement 类型的对象。InlineUIContainer继承自Inline
* 以下举例如何插入图片
*/
private void btnUIElement_Click( object sender, RoutedEventArgs e)
{
Image img = new Image();
img.source = new BitmapImage( new Uri( " /Resource/logo.jpg " , UriKind.Relative));
img.Width = 100 ;
img.Height = 100 ;
InlineUIContainer container = new InlineUIContainer();
container.Child = img;
richTextBox.Selection.Insert(container);
}
/*
* RichTextBox.Xaml - RichTextBox 中的内容的 xaml 代码
* 以下举例如何在“显示模式”和“代码模式”之间切换
*/
private void btnXaml_Checked( object sender, RoutedEventArgs e)
{
if (btnXaml.IsChecked.Value)
{
txtXaml.Text = richTextBox.Xaml;
txtXaml.Visibility = Visibility.Visible;
}
else
{
richTextBox.Xaml = txtXaml.Text;
txtXaml.Visibility = Visibility.Collapsed;
}
}
/*
* RichTextBox.IsReadOnly - 指定是否可以在 RichTextBox 中编辑内容
* true - 显示模式
* false - 编辑模式(此模式下可以通过快捷键的方式来支持撤销 Ctrl+Z 和重做 Ctrl+Y 操作)
* 以下举例如何预览 RichTextBox 中的内容(注:只有在显示模式中超链接才生效)
*/
private void btnPreview_Checked( object sender, RoutedEventArgs e)
{
richTextBox.IsReadOnly = btnPreview.IsChecked.Value;
}
private void btnTextPointerDemo_Click( object sender, RoutedEventArgs e)
{
/*
* TextPointer - 表示 RichTextBox 中的一个位置
* 插入位置(Insertion Position)的概念:
* TextPointer 出现在内容中的字符之间则为插入位置
* TextPointer 出现在定义内容结构的元素标记之间,则此位置不是插入位置。例如,两个相邻段落标记(即前一个段落的结束标记与下一个段落的开始标记)之间的位置就是一个有效的 TextPointer 位置,但不是插入位置
* TextPointer.Compareto(TextPointer tp) - 当前 TextPointer 位于指定的 TextPointer 之前则为 -1,之后则为 1,相同则为 0
* TextPointer.IsAtInsertionPosition - 当前 TextPointer 是否是一个插入位置
* TextPointer.GetNextInsertionPosition(LogicalDirection direction) - 获取当前 TextPointer 的按指定的逻辑方向的下一个插入位置
* LogicalDirection - 逻辑方向 [System.Windows.Documents.LogicalDirection 枚举]
* LogicalDirection.Forward - 向前
* LogicalDirection.Backward - 向后
* TextPointer.GetPositionAtOffset(int offset, LogicalDirection direction) - 获取当前 TextPointer 的按指定的逻辑方向偏移了指定值的 TextPointer
* TextPointer.Parent - 包含当前 TextPointer 的容器。比如,文字的 Parent 是 run ;RichTextBox.ContentEnd 的 Parent 是 RichTextBox
*
* RichTextBox.ContentStart - RichTextBox 内容的开头的 TextPointer(肯定不是插入位置)
* RichTextBox.ContentEnd - RichTextBox 内容的结尾的 TextPointer(肯定不是插入位置)
* RichTextBox.GetPositionFromPoint(Point point) - 获取离指定 Point 最近的插入位置
* RichTextBox.Selection - 获取 RichTextBox 中的选中内容,返回一个 TextSelection 类型的对象
* TextSelection.Start - 选中内容的开头 TextPointer
* TextSelection.End - 选中内容的开头 TextPointer
* TextSelection.Select(TextPointer tp1, TextPointer tp2) - 指定两个 TextPointer 来更新选中内容
*
* 在 RichTextBox 中被视为一个符号的有:TextElement 的开始或结束标记;InlineUIContainer 中的 UIElement 元素;Run 中的字符
*/
// 将光标当前所在位置到文本结尾的文字全变为绿色
TextPointer currentPointer = richTextBox.Selection.Start;
TextPointer endPointer = richTextBox.ContentEnd.GetNextInsertionPosition(LogicalDirection.Backward);
richTextBox.Selection.Select(currentPointer, endPointer);
richTextBox.Selection.ApplyPropertyValue(Run.ForegroundProperty, Colors.Green);
// 将光标当前所在位置到文本结尾的文字信息输出到页面上
while (currentPointer.Compareto(endPointer) == - 1 )
{
string currentChar = "" ;
TextPointer nextPointer = currentPointer.GetNextInsertionPosition(LogicalDirection.Forward);
if (nextPointer != null )
{
richTextBox.Selection.Select(currentPointer, nextPointer);
if (richTextBox.Selection.Text.Length != 0 )
currentChar = richTextBox.Selection.Text;
}
lblMsg.Text += currentChar;
currentPointer = currentPointer.GetNextInsertionPosition(LogicalDirection.Forward);
}
}
}
}
OK
[源码下载]