稳扎稳打Silverlight(42) - 4.0控件之Viewbox, RichTextBox

稳扎稳打Silverlight(42) - 4.0控件之Viewbox, RichTextBox,第1张

概述[源码下载] 稳扎稳打Silverlight(42) - 4.0控件之Viewbox, RichTextBox 作者: webabcd 介绍 Silverlight 4.0 控件一览: Viewbox - 一个容器控件,其内只能有一个子元素。Viewbox 可以决定其内的子元素如何拉伸、缩放、对齐 RichTextBox - 编辑器。用于显示或编辑文本、超链、图片、UI元素等  在线DEMO ht [源码下载]


稳扎稳打Silverlight(42) - 4.0控件之VIEwBox,RichTextBox

作者: webabcd


介绍
Silverlight 4.0 控件一览:
VIEwBox - 一个容器控件,其内只能有一个子元素。VIEwBox 可以决定其内的子元素如何拉伸、缩放、对齐 RichTextBox - 编辑器。用于显示或编辑文本、超链、图片、UI元素等 

在线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 >

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();
        }
    }
}


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 >

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);
            }
        }
    }
}


OK
[源码下载] 总结

以上是内存溢出为你收集整理的稳扎稳打Silverlight(42) - 4.0控件之Viewbox, RichTextBox全部内容,希望文章能够帮你解决稳扎稳打Silverlight(42) - 4.0控件之Viewbox, RichTextBox所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1026750.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存