WPF custom Rich Text display control in asp.net

Source: Internet
Author: User
Tags flush xmlns

RichTextBox is more powerful, supporting rich text and simple text, and so on, to achieve the same effect as word.

Today, customize a RichTextBox control that supports rich text display.

The code is as follows Copy Code
XAML Code:
<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= "D:designwidth=" >
<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>

Background code:

The code is as follows Copy Code

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>

The interactive logic of 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 ();

}

}

}

Description

1, dependency property declaration: Can be through the control point out.

The code is as follows Copy Code
public static readonly DependencyProperty Textyproperty =
Dependencyproperty.register ("Text", typeof (String),

typeof (Richboxtextshow), new PropertyMetadata (string. Empty,new
Propertychangedcallback (textypropertychanged)
));

2. Conversions between rich text and string strings:

The code is as follows Copy Code

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
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.