Silverlight RichTextBox 自定义富文本框

核心代码:

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;
using System.Collections.ObjectModel;
namespace RichTextBoxDemo
{
publicpartialclass MainPage : UserControl
{
private List<String> list =new List<string>();
private Image selectedImage =new Image();
private CwChoosePic childW;
private CwChooseLink cl;
public MainPage()
{
InitializeComponent();
//绑定字体样式数据
ObservableCollection<FontFamily> fonts =new ObservableCollection<FontFamily>();
fonts.Add(
new FontFamily("Arial"));
fonts.Add(
new FontFamily("Courier New"));
fonts.Add(
new FontFamily("Times New Roman"));
fonts.Add(
new FontFamily("宋体"));
MyComboBox.DataContext
= fonts;
MyComboBox.SelectedIndex
=0;
//绑定字体大小
ObservableCollection<Double> fontsize =new ObservableCollection<double>();
for (int i =8; i <=50; i +=2)
{
fontsize.Add(i);
}
SizeComboBox.DataContext
= fontsize;
SizeComboBox.SelectedIndex
=0;
//为ComboBox绑定颜色画刷
ObservableCollection<SolidColorBrush> brush =new ObservableCollection<SolidColorBrush>();
brush.Add(
new SolidColorBrush(Colors.Red));
brush.Add(
new SolidColorBrush(Colors.Blue));
brush.Add(
new SolidColorBrush(Colors.Green));
this.ColorComboBox.ItemsSource = brush;
this.ColorComboBox.SelectedIndex =0;
}
privatevoid btn_Blod_Click(object sender, RoutedEventArgs e)
{
//字体加粗
FontWeight fw = (FontWeight)this.richTextBox.Selection.GetPropertyValue(TextElement.FontWeightProperty);
if (fw == FontWeights.Bold)
{
this.richTextBox.Selection.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Normal);
}
else
{
this.richTextBox.Selection.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
}
}
privatevoid btn_Italic_Click(object sender, RoutedEventArgs e)
{
//字体加倾斜
FontStyle fs = (FontStyle)this.richTextBox.Selection.GetPropertyValue(TextElement.FontStyleProperty);
if (fs == FontStyles.Italic)
{
this.richTextBox.Selection.ApplyPropertyValue(TextElement.FontStyleProperty, FontStyles.Normal);
}
else
{
this.richTextBox.Selection.ApplyPropertyValue(TextElement.FontStyleProperty, FontStyles.Italic);
}
}
privatevoid MyComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//更改字体样式
this.richTextBox.Selection.ApplyPropertyValue(TextElement.FontFamilyProperty, (FontFamily)MyComboBox.SelectedItem);
}
privatevoid SizeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//更改字体大小
this.richTextBox.Selection.ApplyPropertyValue(TextElement.FontSizeProperty, this.SizeComboBox.SelectedItem);
}
privatevoid ColorComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//更改字体颜色
this.richTextBox.Selection.ApplyPropertyValue(TextElement.ForegroundProperty, (SolidColorBrush) this.ColorComboBox.SelectedItem);
}
privatevoid btn_AddImage_Click(object sender, RoutedEventArgs e)
{
//从ChildWindow控件中获取图片,从而添加到编辑框
childW =new CwChoosePic();
childW.Show();
//在关闭时出发事件
childW.Closed +=new EventHandler(childW_Closed);
}
void childW_Closed(object sender, EventArgs e)
{
Paragraph pg
=new Paragraph();
InlineUIContainer container
=new InlineUIContainer();
container.Child
= childW.GetSelectImage();
pg.Inlines.Add(container);
this.richTextBox.Blocks.Add(pg);
}
privatevoid btn_AddLink_Click(object sender, RoutedEventArgs e)
{
//从ChildWindow控件中获取链接信息,从而添加到编辑框
cl =new CwChooseLink();
cl.Show();
//在关闭时出发事件
cl.Closed +=new EventHandler(cl_Closed);
}
void cl_Closed(object sender, EventArgs e)
{
//增加段落
Paragraph pg =new Paragraph();
pg.Inlines.Add( cl.GetHyperlink());
this.richTextBox.Blocks.Add(pg);
}
}
}


效果截图:


 

posted @ 2010-09-06 20:56  糖拌咸鱼  阅读(1450)  评论(2编辑  收藏  举报