核心代码:
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 { public partial class 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; } private void 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); } } private void 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); } } private void MyComboBox_SelectionChanged(object sender,SelectionChangedEventArgs e) { //更改字体样式 this.richTextBox.Selection.ApplyPropertyValue(TextElement.FontFamilyProperty,(FontFamily)MyComboBox.SelectedItem); } private void SizeComboBox_SelectionChanged(object sender,SelectionChangedEventArgs e) { //更改字体大小 this.richTextBox.Selection.ApplyPropertyValue(TextElement.FontSizeProperty,this.SizeComboBox.SelectedItem); } private void ColorComboBox_SelectionChanged(object sender,SelectionChangedEventArgs e) { //更改字体颜色 this.richTextBox.Selection.ApplyPropertyValue(TextElement.ForegroundProperty,(SolidColorBrush) this.ColorComboBox.SelectedItem); } private void 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); } private void 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); } } }
效果截图: