How to save content of RichTextBox in BlockUIContainer, to RTF file? - c#

I made two richtexbox, one small richtexbox nested in a big richtextbox by using BlockUIContainer, see XAML file below.
<RichTextBox IsDocumentEnabled="True" x:Name="first" HorizontalAlignment="Left" Height="174" Margin="120,136,0,0" VerticalAlignment="Top" Width="254" TextChanged="first_TextChanged" MouseDoubleClick="first_MouseDoubleClick">
<FlowDocument>
<Paragraph>
<Run Text="RichTextBox"/>
</Paragraph>
<BlockUIContainer>
<RichTextBox IsReadOnly="True" x:Name="second" MouseDoubleClick="second_MouseDoubleClick" Width="239" TextChanged="second_TextChanged">
<FlowDocument>
<Paragraph>
<Run Text="RichTextBox1"/>
<Run Language="en" Text="hh"/>
</Paragraph>
</FlowDocument>
</RichTextBox>
</BlockUIContainer>
</FlowDocument>
</RichTextBox>
I want contents of both richtexbox saved in a RTF file. Normally we can use TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd), since now we have nested richtexbox, so i think TextRange is probablly not working. And maybe i will add some images in the richtextbox, so I want to try binary stream, see below. "first" is the name of big richtextbox. THE PATH is the path for RTF file.
FlowDocument document = first.Document;
System.IO.Stream ms = new System.IO.MemoryStream();
System.Windows.Markup.XamlWriter.Save(document, ms);
byte[] data = new byte[ms.Length];
ms.Position = 0;
ms.Read(data, 0, data.Length);
ms.Close();
File.WriteAllBytes(THE PATH, data);
But in the saved RTF file, when i open it, it shows like this,
<FlowDocument PagePadding="5,0,5,0" AllowDrop="True" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> <Paragraph>RichTextBox</Paragraph><Paragraph><Run xml:lang="en">test test test!!!</Run></Paragraph><BlockUIContainer><RichTextBox IsReadOnly="True" Name="second" Width="239"><FlowDocument PagePadding="5,0,5,0" AllowDrop="True"><Paragraph>RichTextBox1<Run xml:space="preserve"> </Run><Run xml:lang="en">hhthis is a small richtextbox</Run></Paragraph><Paragraph><Run xml:lang="en" xml:space="preserve" /></Paragraph></FlowDocument></RichTextBox></BlockUIContainer></FlowDocument>
I donĀ“t want to see XAML plain text in the saved file, I want it to show like a normal word document, see below. and probably also see the border line of the small richtextbox (this border line doesnot show below)
RichTextBox
test test test!!!
RichTextBox1 hhthis is a small richtextbox
What should I do? or nested richtextbox by BlockUIContainer is not possible to show like this?

You can do it by saving it and specifying the format. I omit the unnecessary code, but just replace memorystream with what you currently have.
richTextBox.SelectAll();
richTextBox.Selection.Save(new MemoryStream(), DataFormats.Rtf);
XamlWriter will save it as a XAML format

Related

Finding the best way to show html text in RichTextBlock

I have some string like "Some tmp string with text " or "Some tmp string with text" or with other Html format.
The Requirement is to show this text with the same format in RichTextBlock.
To do this, I tried to make some thing like this:
<RichTextBlock x:Name="rtb">
<Paragraph x:Name="par">
<Run FontStyle="Italic"
FontWeight="Bold">
Some tmp text
</Run>
</Paragraph>
</RichTextBlock>
But it formats all the text in Run element, but not the : "Some tmp string with text "
I tried to make Run elements programmatically and add them to paragraph, but have the same problem.
Any suggestions?

C# windows phone 8 app paragraph

Hello I am new to Windows Phone app development. I am right now developing an app that shows a paragraph of text. I used a scroll viewer in order to show the text. I have put the entire paragraph in to a single text block. the problem is when I run the app the emulator displays only half of the text and the remaining half is not visible how to make all the text inside the paragraph visible??. Thank you
Try setting
TextWrapping="Wrap" on TextBlock
Also make sure scrollviewer size is not set to Auto as it will keep expanding to accommodate the text. Try to give screen width size.
Use:
<ScrollViewer>
<TextBlock Text="Your paragraph here" TextWrapping="Wrap" />
</ScrollViewer>
Hi you can use RichTextBox for it like:
<RichTextBox FontSize="25" Background="Transparent">
<Paragraph>
your text goes here.
</Paragraph>
</RichTextBox>
Just add below code.
XAML Code :
<ScrollViewer Grid.Row="1">
<StackPanel>
<RichTextBox x:Name="richTxtBox" VerticalAlignment="Top" Style="{StaticResource NormalRichTextBoxStyle}"/>
</StackPanel>
</ScrollViewer>
Class Code :-
string str = "***** Your String *****";
Paragraph p = new Paragraph();
Run myRun = new Run();
myRun.Text = str;
p.Inlines.Add(myRun);
richTxtBox.Blocks.Add(p);

System.Windows.Controls.RichTextBox different font per line

I need a specific font/style for each line in a RichTextBox, the problem is that I am using System.Windows.Controls.RichTextBox instead of System.Windows.Forms.RichTextBox and it doesn't have the .Select() and .SelectionFont() methods as the Forms one have.
So my question is: How do I add a line in a System.Windows.Controls.RichTextBox with a specific font/style?
If you are using WPF rich text box in WPF accept FlowDocument. with FlowDocument you simply can set style for each line or world and ....
simple example:
<RichTextBox >
<FlowDocument>
<Paragraph>
<Span FontSize="20">first line</Span>
<LineBreak></LineBreak>
<Span Foreground="Red" FontSize="20">sec line</Span>
</Paragraph>
</FlowDocument>
</RichTextBox>

How to "float" image in XAML (Windows 8 Metro)

Is it possible to do in XAML something like ffloat:left for an image in CSS. I need to create something like this:
with variable image dimensions and text length.
Edit: The text warp around the image is not static in my case, it is create in C# code that returns a list of TextBlock elements (with Runs)
With Silverlight 4 you would achieve this using a RichTextBox:
<RichTextBox TextWrapping="Wrap" IsReadOnly="False">
<Paragraph>
More text here ..
<InlineUIContainer>
<Image Source="abc.jpg"/>
</InlineUIContainer>
more and more text here;
<LineBreak />
</Paragraph>
</RichTextBox>
It looks like Win8 Metro has a RichTextBox, and also has an InlineUIContainer, so something like the above should work!
The solution seems to be using RichTextBlockOverflow and OverflowContentTarget described in this presentation: http://video.ch9.ms/build/2011/slides/APP-914T_Street.pptx
This question seems to be asking for something similar to what you want. The answer here should prove a solution to what you desire.
A summary of the answer is, use a FlowDocument like the following example:
<FlowDocument>
<Paragraph>
<Floater HorizontalAlignment="Left">
<BlockUIContainer>
<Image Source="/FlowDocumentTest;component/dog.png" Width="100" />
</BlockUIContainer>
</Floater>
Here is where the text goes to wrap around the image.
</Paragraph>
</FlowDocument>
Update
As your question states, you are now using some C# code to generate TextBlock/Run Elements, both can be children of a Paragraph object. So simply name your Paragraph like so:
<FlowDocument>
<Paragraph x:Name="textPara">
<Floater HorizontalAlignment="Left">
<BlockUIContainer>
<Image Source="/FlowDocumentTest;component/dog.png" Width="100" />
</BlockUIContainer>
</Floater>
</Paragraph>
</FlowDocument>
Then in C#, add your generated TextBlocks or Runs to the Inlines property of textPara, i.e.
var runToInsert = new Run("Some text to display");
textPara.Inlines.InsertAfter(textPara.Inlines.FirstInline, runToInsert);

How to make <UIElement> interactable or click-able in WPF UI

This is my first day to design UI using WPF. I have looked up MSDN official document of Flow Document and found that I can place an UI control inside a RichTextBox. I did put a button in but found it's not interactable - I cannot click on it as it's grey. And I also tried other controls and they all displayed fine but just don't support interaction. Even a hyperlink doesn't work.
I have searched over internet, the closest question ever asked is about how to make an inside hyperlink click-able: The similar question: C# WPF Text with links
I did the same thing but it didn't work! All component displayed well but just are not able to be clicked.
Here is my XAML code:
<RichTextBox Grid.Row="1" Margin="14.007,31.067,22.011,46.305" Name="rtxtRslt" BorderBrush="White" >
<FlowDocument>
<Section FontSize="15">
<Paragraph>
<Bold>Click on this:</Bold>
<Italic><Hyperlink NavigateUri="http://stackoverflow.com">http://www.jxitc.info</Hyperlink></Italic>
</Paragraph>
<BlockUIContainer>
<Button Click="Button_Click">Also Click On This</Button>
</BlockUIContainer>
</Section>
</FlowDocument>
</RichTextBox>
Can anyone give me some suggestion:
1. is it possible to make it click-able
2. if yes, if I forgot to set any/what attribution of the RichTextBox control?
First off your direct question: how to make the content of the RichTextBox "active". Set the IsDocumentEnabled property to True on the RichTextBox like shown here:
<RichTextBox Grid.Row="1" Margin="14.007,31.067,22.011,46.305" Name="rtxtRslt" BorderBrush="White"
IsDocumentEnabled="True">
<FlowDocument>
<Section FontSize="15">
<Paragraph>
<Bold>Click on this:</Bold>
<Italic>
<Hyperlink NavigateUri="http://stackoverflow.com">http://www.jxitc.info</Hyperlink>
</Italic>
</Paragraph>
<BlockUIContainer>
<Button Click="Button_Click" >Also Click On This</Button>
</BlockUIContainer>
</Section>
</FlowDocument>
</RichTextBox>
Now to the unspoken question: do you have to be in a RichTextBox at all? The fact that there is a special property on the RichTextBox to make embedded UI elements active kinda indicates that is not the normal usage for this control. It is meant to host editable FlowDocument content. So the user of the RichTextBox would typically be creating the document that hosts the button that a consumer of the document could click, if that helps make the distinction clear I don't know.
However, all that being said, your FlowDocument hosted instead in a simple FlowDocumentPageViewer is active by default.
<FlowDocumentPageViewer>
<FlowDocument>
<Section FontSize="15">
<Paragraph>
<Bold>Click on this:</Bold>
<Italic>
<Hyperlink NavigateUri="http://stackoverflow.com">http://www.jxitc.info</Hyperlink>
</Italic>
</Paragraph>
<BlockUIContainer>
<Button Click="Button_Click" >Also Click On This</Button>
</BlockUIContainer>
</Section>
</FlowDocument>
</FlowDocumentPageViewer>
Now to the other unspoken question (unspeakable?) do you have to be in FlowDocument content at all? FlowDocument content is similar to, but not derived from UIElement. As such, many of the out-of-the-box features of UIElements are not available. If you need document functionality in the UI FlowDocuments can provide a great start but bring with them a pretty big learning curve in their own right.
The title of your question, if taken literally, makes me think you might just want a WPF UI that allows you to embed Buttons and Hyperlinks and have them work (gasp). That is certainly the default behavior. If you do not need the document look and feel that FlowDocument provides nor the real time document editing that RichTextBox provides you might consider a more "traditional" WPF layout.
<StackPanel Orientation="Vertical" HorizontalAlignment="Stretch"
Margin="20">
<TextBlock>
<Bold>Click on this:</Bold>
<Italic>
<Hyperlink NavigateUri="http://stackoverflow.com">http://www.jxitc.info</Hyperlink>
</Italic>
</TextBlock>
<Button Click="Button_Click"
Margin="0,20,0,0">Also Click On This</Button>
</StackPanel>

Categories