asp.net中WPF自訂富文本顯示控制項

來源:互聯網
上載者:User

RichTextBox比較的強大,支援富文本和簡單文本等,可以實現出類似Word的那樣的效果。

今天自訂一個支援富文本顯示的RichTextBox控制項。

 代碼如下 複製代碼
XAML代碼:
<UserControl x:Class="Kaitone.DetectiveHelper.UI.Controls.RichTextBox.RichboxTextShow"
     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">
    <Grid>
<RichTextBox Name="mainRTB" AcceptsReturn="True" IsReadOnly="True"
     BorderBrush="Transparent" BorderThickness="0" Background="Transparent"
     HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
     VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
    
</RichTextBox>
    </Grid>
</UserControl>

後台代碼:

 代碼如下 複製代碼

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

{

    /// <summary>

    /// RichboxTextShow.xaml 的互動邏輯

    /// </summary>

    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();

}

    }

}

說明:

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.