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


    效果截图:


  • 相关阅读:
    centos 安装python3.6 简易安装过程
    包装类型
    剑指offer二叉树中和为某一值的路径
    剑指offer 捡绳子
    按位与的技巧
    SpringBoot 登录拦截功能的实现
    AIO实现简单http服务器
    真题-数的分解
    重建二叉树
    旋转数组的最小数字
  • 原文地址:https://www.cnblogs.com/coser/p/1968814.html
Copyright © 2020-2023  润新知