How to change parts of the string in C# - 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

Related

Create C#/WPF Button using a Wingding or Webding font Character [duplicate]

I'm trying to do this:
<TextBlock Text="{Binding Path=Text,
Converter={StaticResource stringFormatConverter},
ConverterParameter='&\u2014{0}'}" />
To get a — to appear in front of the text. It doesn't work. What should I be doing here?
Since XAML is an XML file format you could try the XML character escape. So instead of writing &\u2014, you could write — instead.
In xaml I did it like this:
<Button Grid.Column="1" Grid.RowSpan="2" Name="start" Margin="5" Click="start_Click">
<TextBlock Name="test" FontFamily="pack://application:,,,/Y_Yoga;Component/Resources/#FontAwesome"></TextBlock>
</Button>
Hope to be helpful!
I came to this page for some other reason, but this does not include the easiest and the obvious solution.
This is what I do.
Maintain a static class with all the Unicode values.
public static class Icons
{
public const string IconName = "\u2014";
}
And then just bind it wherever you need it.
<TextBlock Text="{x:Static resources:Icons.IconName}" FontFamily="..."/>
This also helps you out with maintenance, all icons would be in one place to manage.
From Microsoft documentation:
Markup files that are created in Microsoft Visual Studio are
automatically saved in the Unicode UTF-8 file format, which means that
most special characters, such as accent marks, are encoded correctly.
However, there is a set of commonly-used special characters that are
handled differently. These special characters follow the World Wide
Web Consortium (W3C) XML standard for encoding.
What this means is that you can do zalgo for all you care
Bit of code that is relevant:
<Label Grid.Column="0" Grid.Row="3" FontWeight="ExtraBlack">STAGE:Mͣͭͣ̾ Vͣͥͭ͛ͤͮͥͨͥͧ̾</Label>
Save the file as UTF-8. In Visual Studio, you can do this by going "File" → "Advanced Save Options".
The UTF-32 syntax in WPF/XAML is like so:   󱬊
<TextBlock Text="foo 󱬊 bar"
FontFamily="Material Design Icons" />
This example is using the Material Design Icons desktop font (glyph dump here)

Binding list of paragraphs to RichTextBlock

Right now, I have the data I need binding to a RichTextBlock, but I'm unable to format it, since I'm just using one long string bound to the run on a paragraph setup in my RichTextBlock. Obviously, this isn't ideal. I would like to be able to change font sizes or font weights for some words, but can't see of a way to do that with my current method.
<RichTextBlock TextAlignment="Justify" TextWrapping="WrapWholeWords" Name="richTB">
<Paragraph>
<Run Text="{Binding}" />
</Paragraph>
</RichTextBlock>
I know I can create a RichTextBlock in my code behind, and add paragraphs manually to that, but how would I bind it? Since it's in my page.resources?
Unfortunately you can't really bind it if you want to do custom formatting on the string. XAML builds those controls, and you can't really generate those controls through a binding the way you want.
What you could do is hook up an OnLoad event handler for your control, and inject whatever paragraph/run controls via C#.
I believe you are {binding} the source and hence inside the Code, you can take help from this code:
Paragraph para = new Paragraph();
para.Inlines.Add(new Run { Text = sample, FontSize = 15 });
rtb.Blocks.Add(para);
where rtb is a richtextblock.

MaskedInput in Windows Phone

I'm trying to implement MaskedInput TextBox at WP8. The common scenario: empty field, user see mask, e.g. ###-##-##. Then he starts to input values: 1##-##-##, 21#-##-##, 321-##-##, 432-1#-## and so on. I thought about using TextSelection property and TextChanged event to maintain right selection position and on-fly mask replacing with values. The problem is that I can't use different color for mask, as it share the same input with value. Also I can't use another TextBox or TextBlock behind active, because mask characters will have different width with value (e.g. ### will not align with W##). Is there some way to support multiple foreground at TextBox or to adjust characters width to same value?
UPD: Well, I know about monospacing fonts. Guess, it's the only way to get what I need. But is there some way to implement that feature in one TextBox?
Yes, You will Use like this.
<TextBlock>
<Run Text="*" Foreground="#FFE10101"/><Run Text="Required Line" />
<Run Text="Red" Foreground="Red"/>
<Run Text="Blue" Foreground="Blue"/>
<Run Text="{Binding yourString, StringFormat='with \{0\} a different color!'}" Foreground="Orange"/>
</TextBlock>
For More Reference Refer this Link

Set TextBlock to preserve white space at the beginning and at the end?

EDIT:
The code below actually works as I want - this question a little misleading. Please ignore it.
Normally when I set Text property of TextBlock like this:
TextBlock tb = new TextBlock();
tb.Text = " Hello World ";
The whitespace at the beginning and at the end of text are not shown. The text shown by TextBlock is only Hello World. How can I set TextBlock to display them (i.e., not remove the whitespace)? Am I missing some property?
In this case you don't need to use xml:space="preserve"
<TextBlock xml:space="preserve" Text=" Hello world! " />
WILL display the whitespaces, however
<TextBlock> Hello world! </TextBlock>
won't.
Re: "I just hope you are not using this to align your text. There are many other more elegant methods to do so."
Sounds like you might want to use the Padding property:
http://msdn.microsoft.com/en-us/library/system.windows.controls.textblock.padding(VS.85).aspx.
See also the various alignment & margin properties.
set the xml:space property to preserve in your XAML, i assume you are using WPF
<TextBlock xml:space="preserve" Text=" Hello world! " />
EDIT: It is sometimes easier to do things in XAML. I just hope you are not using this to align your text. There are many other more elegant methods to do so.
You don't need to use the Text= property. This also works:
<TextBlock xml:space="preserve">Staff Contact Details <Hyperlink>Click here</Hyperlink></TextBlock>

Making URLs on a textblock clickable

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.

Categories

Resources