asp.net中WPF自定义富文本显示控件

作者:袖梨 2022-06-25

RichTextBox比较的强大,支持富文本和简单文本等,可以实现出类似Word的那样的效果。

今天自定义一个支持富文本显示的RichTextBox控件。

 代码如下 复制代码
XAML代码:
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
     mc:Ignorable="d"
     d:DesignHeight="300" d:DesignWidth="300">
   
     BorderBrush="Transparent" BorderThickness="0" Background="Transparent"
     HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
     VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
    

   


后台代码:

 代码如下 复制代码

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;

 

namespace Kaitone.DetectiveHelper.UI.Controls.RichTextBox

{

    ///

    /// RichboxTextShow.xaml 的交互逻辑

    ///

    public partial class RichboxTextShow : UserControl

    {

public static readonly DependencyProperty TextyProperty =

    DependencyProperty.Register("Text",typeof(string),

   

    typeof(RichboxTextShow),new PropertyMetadata(string.Empty,new

PropertyChangedCallback(TextyPropertyChanged)

));

private static void TextyPropertyChanged(DependencyObject sender,DependencyPropertyChangedEventArgs args)

{

    RichboxTextShow editer = new RichboxTextShow();

  

}

public string Text

{

    get { return ConvertToRtfString(this.mainRTB); }

    set {

if (!String.IsNullOrEmpty(value))

{

    LoadFromRtfString(value,this.mainRTB);

}

else

{

    this.mainRTB.Document.Blocks.Clear();

}

    }

}

private static void LoadFromRtfString(string rtf, System.Windows.Controls.RichTextBox richTextBox)

{

    if (string.IsNullOrEmpty(rtf))

    {

throw new ArgumentNullException();

    }

    TextRange textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);

    using (MemoryStream ms = new MemoryStream())

    {

using (StreamWriter sw = new StreamWriter(ms))

{

    sw.Write(rtf);

    sw.Flush();

    ms.Seek(0, SeekOrigin.Begin);

    textRange.Load(ms, DataFormats.Rtf);

}

    }

      

}

private static string ConvertToRtfString(System.Windows.Controls.RichTextBox rcb)

{

    string resultstring = string.Empty;

    using (MemoryStream stream=new MemoryStream())

    {

TextRange documentTextRange = new TextRange(rcb.Document.ContentStart, rcb.Document.ContentEnd);

string dataformt = DataFormats.Rtf;

documentTextRange.Save(stream, dataformt);

stream.Seek(0, SeekOrigin.Begin);

using(StreamReader reader=new StreamReader(stream,Encoding.Default)){

    resultstring = reader.ReadToEnd();

}

 

    }

    return resultstring;

}

public RichboxTextShow()

{

    InitializeComponent();

}

    }

}

说明:

asp.net中WPF自定义富文本显示控件

1、依赖属性声明:可以通过控件点出来的。

 代码如下 复制代码
public static readonly DependencyProperty TextyProperty =
    DependencyProperty.Register("Text",typeof(string),
    
    typeof(RichboxTextShow),new PropertyMetadata(string.Empty,new
PropertyChangedCallback(TextyPropertyChanged)
));

2、富文本和字符串String之间的转换:

 代码如下 复制代码

private static void LoadFromRtfString(string rtf, System.Windows.Controls.RichTextBox richTextBox)
{
    if (string.IsNullOrEmpty(rtf))
    {
throw new ArgumentNullException();
    }
    TextRange textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
    using (MemoryStream ms = new MemoryStream())
    {
using (StreamWriter sw = new StreamWriter(ms))
{
    sw.Write(rtf);
    sw.Flush();
    ms.Seek(0, SeekOrigin.Begin);
    textRange.Load(ms, DataFormats.Rtf);
}
    }

}
private static string ConvertToRtfString(System.Windows.Controls.RichTextBox rcb)
{
    string resultstring = string.Empty;
    using (MemoryStream stream=new MemoryStream())
    {
TextRange documentTextRange = new TextRange(rcb.Document.ContentStart, rcb.Document.ContentEnd);
string dataformt = DataFormats.Rtf;
documentTextRange.Save(stream, dataformt);
stream.Seek(0, SeekOrigin.Begin);
using(StreamReader reader=new StreamReader(stream,Encoding.Default)){
    resultstring = reader.ReadToEnd();
}
    }
    return resultstring;
}

相关文章

精彩推荐