Silverlight: RichTextBox的简单学习

RichTextBox的简单使用

其实在Silverlight 4 中的sample中已经有一个很漂亮的名叫TextEditor的RichTextBox控件,

Run Silverlight TextEditor

Download the Sample

以下是我自己简单封装的一个RichEditor,返回的是一个UIElement.

 

1.  效果图

   2010043013563793.jpg

 

2.  调用方法    

ContractedBlock.gif ExpandedBlockStart.gif RichEditor 调用代码
 
    
RichEditor editor = new Controls.RichEditor();
// 根据需要设置是否显示操作条
editor.IsDiplayClipboardBar = System.Windows.Visibility.Visible;
editor.IsDiplayImageBar
= Visibility.Visible;
editor.IsDiplayParagraphBar
= Visibility.Visible;
AutoGrid element
= editor.GenerateRichEditor();

TaskDialog dialog
= new TaskDialog()
{
Content
= new Border
{
Child
= new AutoGrid( 2 , 80 , 700 )
{
HorizontalAlignment
= System.Windows.HorizontalAlignment.Left,
Children
= {
(
new TextBlock(){Text = " Title: " }),( new TextBox()),
(
new TextBlock(){Text = " SubTitle: " }),( new TextBox()).Do(o => {o.Margin = new Thickness ( 0 , 5 , 0 , 5 );}),
(
new TextBlock(){Text = " Author: " }),( new TextBox()),
(
new TextBlock(){Text = " Content: " }).GridRowSpan( 2 ).Do(o => {o.Margin = new Thickness ( 0 , 45 , 0 , 0 );}),
element.Do(o
=> {o.Margin = new Thickness ( 0 , 5 , 0 , 5 );}),
}
}
}
};
dialog.OK
+= (s, args) => {
// UI.Alert(((RichTextBox)rtb.RichTextBoxContent).Xaml.ToString());
if (editor.GetRichEditor() != null )
{
UI.Alert(editor.GetRichEditor().Xaml.ToString());
}
};
dialog.Show();

  

3.  主要方法   

ContractedBlock.gif ExpandedBlockStart.gif 为字体设置为斜体
 
    
DependencyProperty property = TextElement.FontStyleProperty;
var value
= richEditor.Selection.GetPropertyValue(property);
var newValue
= FontStyles.Italic;
if (value != DependencyProperty.UnsetValue && ((FontStyle)value) == FontStyles.Italic)
{
newValue
= FontStyles.Normal;
}
richEditor.Selection.ApplyPropertyValue(property, newValue);

在以上,首先取得选择的要设置字体的格式的文字,然后稍加判断是否需要设置格式,然后为其设置格式即可。

在有些时候我们需要这样使用

DependencyProperty property = Inline.TextDecorationsProperty;

有些时候我们也可以直接使用Run,在RichTextBox生成的Xaml代码中我们就可以很清楚的看到。

 

 

4.  相关问题(本人没搞明白)

   如何获取操作系统中现有的字体、大小然后遍历出来提供给用户?

   这里的图片不是url,不是url就不能保存了?

   恳切希望看到此问题的人不吝赐教,谢谢!

 

   

转载于:https://www.cnblogs.com/blackcore/archive/2010/04/30/1722167.html