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?
Related
How do I achieve formatting of a text inside a TextBlock control in my WPF application?
e.g.: I would like to have certain words in bold, others in italic, and some in different colors, like this example:
The reason behind my question is this actual problem:
lblcolorfrom.Content = "Colour From: " + colourChange.ElementAt(3).Value.ToUpper();
I would like the second part of the string to be bold, and I know that I could use two controls (Labels, TextBlocks, etc.) but I'd rather not, due the vast amount of controls already in use.
You need to use Inlines:
<TextBlock.Inlines>
<Run FontWeight="Bold" FontSize="14" Text="This is WPF TextBlock Example. " />
<Run FontStyle="Italic" Foreground="Red" Text="This is red text. " />
</TextBlock.Inlines>
With binding:
<TextBlock.Inlines>
<Run FontWeight="Bold" FontSize="14" Text="{Binding BoldText}" />
<Run FontStyle="Italic" Foreground="Red" Text="{Binding ItalicText}" />
</TextBlock.Inlines>
You can also bind the other properties:
<TextBlock.Inlines>
<Run FontWeight="{Binding Weight}"
FontSize="{Binding Size}"
Text="{Binding LineOne}" />
<Run FontStyle="{Binding Style}"
Foreground="Binding Colour}"
Text="{Binding LineTwo}" />
</TextBlock.Inlines>
You can bind through converters if you have bold as a boolean (say).
You can do this in XAML easily enough:
<TextBlock>
Hello <Bold>my</Bold> faithful <Underline>computer</Underline>.<Italic>You rock!</Italic>
</TextBlock>
There are various Inline elements that can help you, for the simplest formatting options you can use Bold, Italic and Underline:
<TextBlock>
Sample text with <Bold>bold</Bold>, <Italic>italic</Italic> and <Underline>underlined</Underline> words.
</TextBlock>
I think it is worth noting, that those elements are in fact just shorthands for Span elements with various properties set (i.e.: for Bold, the FontWeight property is set to FontWeights.Bold).
This brings us to our next option: the aforementioned Span element.
You can achieve the same effects with this element as above, but you are granted even more possibilities; you can set (among others) the Foreground or the Background properties:
<TextBlock>
Sample text with <Span FontWeight="Bold">bold</Span>, <Span FontStyle="Italic">italic</Span> and <Span TextDecorations="Underline">underlined</Span> words. <Span Foreground="Blue">Coloring</Span> <Span Foreground="Red">is</Span> <Span Background="Cyan">also</Span> <Span Foreground="Silver">possible</Span>.
</TextBlock>
The Span element may also contain other elements like this:
<TextBlock>
<Span FontStyle="Italic">Italic <Span Background="Yellow">text</Span> with some <Span Foreground="Blue">coloring</Span>.</Span>
</TextBlock>
There is another element, which is quite similar to Span, it is called Run. The Run cannot contain other inline elements while the Span can, but you can easily bind a variable to the Run's Text property:
<TextBlock>
Username: <Run FontWeight="Bold" Text="{Binding UserName}"/>
</TextBlock>
Also, you can do the whole formatting from code-behind if you prefer:
TextBlock tb = new TextBlock();
tb.Inlines.Add("Sample text with ");
tb.Inlines.Add(new Run("bold") { FontWeight = FontWeights.Bold });
tb.Inlines.Add(", ");
tb.Inlines.Add(new Run("italic ") { FontStyle = FontStyles.Italic });
tb.Inlines.Add("and ");
tb.Inlines.Add(new Run("underlined") { TextDecorations = TextDecorations.Underline });
tb.Inlines.Add("words.");
Check out this example from Charles Petzolds Bool Application = Code + markup
//----------------------------------------------
// FormatTheText.cs (c) 2006 by Charles Petzold
//----------------------------------------------
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Documents;
namespace Petzold.FormatTheText
{
class FormatTheText : Window
{
[STAThread]
public static void Main()
{
Application app = new Application();
app.Run(new FormatTheText());
}
public FormatTheText()
{
Title = "Format the Text";
TextBlock txt = new TextBlock();
txt.FontSize = 32; // 24 points
txt.Inlines.Add("This is some ");
txt.Inlines.Add(new Italic(new Run("italic")));
txt.Inlines.Add(" text, and this is some ");
txt.Inlines.Add(new Bold(new Run("bold")));
txt.Inlines.Add(" text, and let's cap it off with some ");
txt.Inlines.Add(new Bold(new Italic (new Run("bold italic"))));
txt.Inlines.Add(" text.");
txt.TextWrapping = TextWrapping.Wrap;
Content = txt;
}
}
}
a good site, with good explanations:
http://www.wpf-tutorial.com/basic-controls/the-textblock-control-inline-formatting/
here the author gives you good examples for what you are looking for! Overal the site is great for research material plus it covers a great deal of options you have in WPF
Edit
There are different methods to format the text. for a basic formatting (the easiest in my opinion):
<TextBlock Margin="10" TextWrapping="Wrap">
TextBlock with <Bold>bold</Bold>, <Italic>italic</Italic> and <Underline>underlined</Underline> text.
</TextBlock>
Example 1 shows basic formatting with Bold Itallic and underscored text.
Following includes the SPAN method, with this you van highlight text:
<TextBlock Margin="10" TextWrapping="Wrap">
This <Span FontWeight="Bold">is</Span> a
<Span Background="Silver" Foreground="Maroon">TextBlock</Span>
with <Span TextDecorations="Underline">several</Span>
<Span FontStyle="Italic">Span</Span> elements,
<Span Foreground="Blue">
using a <Bold>variety</Bold> of <Italic>styles</Italic>
</Span>.
</TextBlock>
Example 2 shows the span function and the different possibilities with it.
For a detailed explanation check the site!
Examples
This is my solution....
<TextBlock TextWrapping="Wrap" Style="{DynamicResource InstructionStyle}">
<Run Text="This wizard will take you through the purge process in the correct order." FontWeight="Bold"></Run>
<LineBreak></LineBreak>
<Run Text="To Begin, select" FontStyle="Italic"></Run>
<Run x:Name="InstructionSection" Text="'REPLACED AT RUNTIME'" FontWeight="Bold"></Run>
<Run Text="from the menu." FontStyle="Italic"></Run>
</TextBlock>
I am learning... so if anyone has thaughts on the above solution please share! :)
I'm creating an Windows 8.1 Store Application, and I need to distribute a string over a RichTextBlock and an RichtTextBlockOverflow but I can't find a solution on the internet.
Any help would be very nice!
In XAML
<RichTextBlock>
<RichTextBlock.Blocks>
<Paragraph>
<Paragraph.Inlines>
<Run Text="Some Text" />
</Paragraph.Inlines>
</Paragraph>
</RichTextBlock.Blocks>
</RichTextBlock>
In Code
Dim p As New Paragraph
p.Inlines.Add(New Run With {.Text = "Some text"})
Dim myRichTxtBox As New RichTextBlock
myRichTxtBox.Blocks.Add(p)
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
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>
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);