This is a migrated thread and some comments may be shown as answers.

Inheriting styles when pasting

1 Answer 97 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Wil
Top achievements
Rank 1
Wil asked on 26 Mar 2014, 06:31 PM
Hello,

Is there a way to inherit the current font settings when pasting text into a RadRichTextBox that was copied from another RadRichTextBox?

If the destination has the text "original text" with Arial font and the source has the text "testing" with no font specified, copying and pasting after "original" should show "original testing text" with Arial font.

The following is what I'm trying:

XAML:
<t:RadRichTextBox Name="txt" Grid.Row="1" FontFamily="inherited" DocumentInheritsDefaultStyleSettings="True"/>
<TextBox Name="html" Grid.Row="2" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"/>


Code Behind:

01.public MainWindow()
02.{
03.    InitializeComponent();
04. 
05.    txt.ChangeParagraphSpacingAfter(0);
06.    txt.DocumentContentChanged += TxtOnDocumentContentChanged;
07.}
08. 
09.private void TxtOnDocumentContentChanged(object sender, EventArgs eventArgs)
10.{
11.    UpdateHtml();
12.}
13. 
14.private void UpdateHtml()
15.{
16.    var settings = new HtmlExportSettings();
17.    settings.DocumentExportLevel = DocumentExportLevel.Fragment;
18.    settings.ExportStyleMetadata = false;
19.    settings.StylesExportMode = StylesExportMode.Inline;
20. 
21.    var converter = new HtmlFormatProvider();
22.    converter.ExportSettings = settings;
23.    html.Text = converter.Export(txt.Document);
24.}

The output shows the following:
<p class="Normal "><span style="font-family: 'Arial';">Original </span>testing<span style="font-family: 'Arial';"> text</span></p>

This causes testing to still be "inherited" since it is not inserted as a child of the span with "Original".  Is there any way I can do this or do I need to take over the Paste logic?


Thanks,
Wil

1 Answer, 1 is accepted

Sort by
0
Petya
Telerik team
answered on 31 Mar 2014, 12:23 PM
Hi Wil,

When executing PasteCommand the way the paste happens depends on the source from which you copied the content in the first place. Basically, three of the format providers - RtfFormatProvider, HtmlFormatProvider and TxtFormatProvider, are responsible for determining the proper format and executing the paste.

I tried the setup you explained and as far as I can tell when you copy plain text (for example, from Notepad) pasting it in the control causes the text to inherit the styling from the previous content. However, if you copy from a browser or from MS Word, the fragment in the clipboard already has style.

If you want to you can modify the behavior. Please refer to the Clipboard Support article for more information on the matter.

I hope this helps.

Regards,
Petya
Telerik
 

Build cross-platform mobile apps using Visual Studio and .NET. Register for the online webinar on 03/27/2014, 11:00AM US ET.. Seats are limited.

 
Tags
RichTextBox
Asked by
Wil
Top achievements
Rank 1
Answers by
Petya
Telerik team
Share this question
or