Making URLs on a textblock clickable - c#

I'm programming a little Twitter Client just for fun. I have the tweet's text on a TextBlock and I want to make the URLs clickable.
I know that I have to parse the URLs with a regexp but... how I put text plus link on the TextBlock?
I can't have a string like: Hello check my blog at <Hyperlink>http​://myblogurl.com</Hyperlink> because the TextBlock doesn't parse the tags.
Then, how I can have a TextBlock that maybe has a link or maybe not?
Thank you.

<RichTextBox IsDocumentEnabled="True">
<FlowDocument>
<Paragraph>
This is a richTextBox. And this is a <Hyperlink NavigateUri="http://www.microsoft.com">Hyperlink</Hyperlink>.
</Paragraph>
</FlowDocument>
</RichTextBox>
MSDN discussion

Rather than use a TextBlock, take a look at using the WPF version of the RichTextBox. It's a very flexible little critter.

Something like...
<TextBlock>
<Hyperlink Name="btnOpen" Click="btnOpen_Click">
<TextBlock Text="Click to Open" />
</Hyperlink>
</TextBlock>

You could parse the string in code behind and build up a collection of content controls, change your textblock to a wrap panel and set the children of the panel to your collection you have created.

Related

WPF consuming much time to paste large texts to RichTextBox from clipboard

I copied roughly a 200,000 length text to the Clipboard.
And I pasted it to RichTextBox by using shortcut key Ctrl V.
My pc couldn't load it..
But, when I pasted it to a TextBox, it worked well.
Can I ask you what problem is?
Here is my code snippet.
<RichTextBox Grid.Row="1" x:Name="에디터"
AcceptsTab="True" AcceptsReturn="True"
ScrollViewer.VerticalScrollBarVisibility="Auto"
PreviewMouseLeftButtonUp="에디터_PreviewMouseLeftButtonUp"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
Margin="5"
>
<FlowDocument Name="editFlow" >
<Paragraph>
여기에 텍스트를 붙여 넣으세요
</Paragraph>
</FlowDocument>
</RichTextBox>
You cannot do it and you should not do it. WPF controls aren't designed to work with that kind of data(hence it freezes).Lets say even if you had succeded in adding that data in RTB the scrollbars would be useless as there would be very little data to display.Imagine the pains you will have when you have to locate a line in the text using scrollbar (it could take minutes...).
So the solution is you need to rethink what you are doing and maybe change your logic.

Change text of textblock within a panoramaitem

I need to use two textblock within a single panorama item. I have used a grid to implement this.
Problem is that I need to change the text of the textblock programmatically.
<phone:PanoramaItem Header="Current Status">
<Grid >
<TextBlock x:Name="Spent" Margin="138,0,90,464" FontSize="40"/>
<TextBlock x:Name="Left" Margin="138,50,90,414" FontSize="40"/>
</Grid>
</phone:PanoramaItem>
Saying:
Spent.Text = Convert.ToString(total_spent);
Left.Text = Convert.ToString(total_left);
in the c# file gives an error.
Please tell me how to change the text of the individual textblocks :)
P.S:
I am an absolute beginner and almost completely self taught, so a simple answer will be useful
Thanks
You need to use Text property of the TextBlock to get/set the Text.
From MSDN : TextBlock.Text
Gets or sets the text contents of a TextBlock.
Try This:
Spent.Text = Convert.ToString(total_spent.Text);
Left.Text = Convert.ToString(total_left.Text);

How to change parts of the string in C#

I am writing a c# wpf application that get information from rss feed. I got a string with "<'b>Bold Text<'/b> and <'i>Italic Text<'/i> and <'u>Underline text<'/u>" and need to change the font styles to the correct style when I want to use the string for a label or textbox. The output now would be like Label: <'b>Text<'/b> instead of Text being BOLD. Need help to find a way to fix it.
Let's say you got the following text:
<b>Bold</b><i>Italic</i>
You need to break the texts up and put them into WPF tags. That text in WPF must look like this:
<TextBlock>
<Run Text="Bold" FontWeight="Bold" />
<Run Text="Italic" FontStyle="Italic" />
</TextBlock>
If you google HTML to XAML convertion, you should find some API's or examples you can use:
XAML to HTML Conversion Demo
HTML-XAML Converter

how to add emoticon in RichTextBox in WPF [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to add emoticons(smileys) into WPF richtext box
Now,I want to know , how to add emoticon in RichTextBox in WPF.
For example: when I type hi :) , it will return as image(smile image) in RichTexBox.
I hope , you will help me. if anyone know the solution or example code
Thanks
There isn't a prebuilt solution for this, but, as you're using WPF, you can either react to change of the text inside the RichTextBox via the TextChanged event or via your PropertyChanged -if it's MVVM-.
Once you know text changed you give it a pass by using a simple search (there are way better ways to do this parsing, but for the sake of a simple example just use a simple string search). If you find the text you want, I mean, the token corresponding your smiley hi :) then you can insert the image on that position by playing with the FlowDocument inside the RichTextBox
Before parsing
<RichTextBox>
<FlowDocument>
<Paragraph>
<Run>Hello :) world!</Run>
</Paragraph>
</FlowDocument>
</RichTextBox>
After parsing
<RichTextBox>
<FlowDocument>
<Paragraph>
<Span>
<Run Text="Hello"/>
<Image Width="16" Source="Your emoticon source"/>
<Run Text=" world"/>
</Span>
</Paragraph>
</FlowDocument>
</RichTextBox>
That should give you an idea of how to begin.

How to get text fit in text block in WPF

I have string which i have to display in TextBlock, my TextBlock have some fixed size, i need display the text in such manner if string cannot fit in TextBlock, then i have to split the string in next TextBlock, how can i do this same.
Why don't you try using the TextWrapping property of that TextBlock?
XAML:
<TextBlock TextWrapping="Wrap" Text="very very very long text" Width="30"/>
C#:
myTextBlock.TextWrapping = TextWrapping.Wrap;
If you don't want wrapping, then slapping on a horizontal/vertical scrollbar is another option that you may want to explore. Reading the question I think textwrapping might be more appropriate (doesn't sound like you want to hide anything), but options are always nice.
<ScrollViewer Height="30">
<TextBlock Width="30" TextWrapping="Wrap">HElooooooooooooooooooooooooooooooooooooo</TextBlock>
</ScrollViewer>
EDIT: Combines a word wrap and a scrollviewer.

Categories

Resources