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


    效果截图:


  • 相关阅读:
    rpm命令参数(转载)
    通过KMS服务器激活windows
    icehouse版本中常用操作命令
    openstack环境搭建常用命令
    openstack 重启服务命令
    python连接mysql数据库报错pymysql连接数据库报错TypeError: __init__() takes 1 positional argument but 5 positional arguments
    Linux下LDAP统一认证解决方案
    windows系统添加IP地址黑名单
    网站防止SQL注入
    Windows服务器修改远程端口号的方法
  • 原文地址:https://www.cnblogs.com/coser/p/1968814.html
Copyright © 2020-2023  润新知