How to dynamically display formatted text in TextBlock wpf - wpf

I have some text in a resx file that I need to display in a TextBlock. This text will have Bold areas that I will have to show in Bold. What is the best way to maintain Bold areas when the text changes? I cannot do inline from code behind because the text in resx file can change. WPF vb.net application. Thanks

You need to use Inlines:
<TextBlock.Inlines>
<Run FontWeight="Bold" FontSize="14" Text="{Binding BoldTextProperty}" />
<Run FontStyle="Italic" Foreground="Red" Text="{Binding ItalicandRedColorTextProperty}" />
</TextBlock.Inlines>
If you are not using binding, assign the text directly to the Text property.
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).

I had to make some changes. I removed the text from resx file and put it into a .txt file instead. I created a .txt file for each body under a header and hard coded the headers. Using Inline from code behind I controlled what goes in the header and what goes under it as the body. This way body of the text can be changed in the .txt files and code will stay the same.

Related

WPF : Can we make groupbox header editable at runtime

i want groupbox's header editable that means at runtime i want it to be open for edit like we do rename in microsoft excelsheet in bottom left.
i came to know we can do it by control template but don't know how to write template for it.
It's easy to create Editable Header:
<GroupBox>
<GroupBox.HeaderTemplate>
<DataTemplate>
<TextBox Text="Editable Header"/>
</DataTemplate>
</GroupBox.HeaderTemplate>
<TextBlock Text="Some Content" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</GroupBox>
Or if you want to modify the template of all control?
Then you can find sample here: https://msdn.microsoft.com/en-us/library/vstudio/ms744748(v=vs.100).aspx

wpf set property to static resource with string manipulation

I'm creating a WPF application. It has two labels which use the same static string resource, but with some differences. For example, it has a string resource with the key string1 and value SuccessRate. I want the first label to be SuccessRate and the second Label to be SuccessRate(%). I define the first label with:
<Label Content="{StaticResource string1}" />
How can I define the second Label?
You might set the Content of the second Label to a TextBlock with two Run elements:
<Label>
<TextBlock>
<Run Text="{StaticResource string1}"/>
<Run Text="(%)"/>
</TextBlock>
</Label>
Perhaps you need to have only TextBlocks instead of Labels anyway:
<TextBlock Text="{StaticResource string1}"/>
<TextBlock>
<Run Text="{StaticResource string1}"/>
<Run Text="(%)"/>
</TextBlock>
You can use ContentStringFormat
<Label Content="{StaticResource string1}" ContentStringFormat="{}{0}(%)" ... />
Note that format starts with {}. It's just something what has to be there if your format starts with. {
You can read about ContentStringFormat on MSDN.

Silverlight TextBlock TextTrimming inside ContentControl disappears

I'm displaying a series of messages (like emails) on a Grid:
<layout:TransitioningContentControl Name="tccCmdMessage" Margin="0,4">
<layout:TransitioningContentControl.ContentTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Title}" FontWeight="SemiBold" />
<TextBlock Name="tbCmdMessage" Text="{Binding Message}" TextTrimming="WordEllipsis" />
</StackPanel>
</DataTemplate>
</layout:TransitioningContentControl.ContentTemplate>
</layout:TransitioningContentControl>
However, the tbCmdMessage never displays. If I remove the TextTrimming (or change it to None) it works. Alternatively if I don't use a ContentControl parent it also works.
Any ideas?
Take a look at this link: http://social.msdn.microsoft.com/Forums/eu/wpf/thread/30fd3279-7bc8-424f-9ee6-41b9f9589a1a.
I suppose explicitly specifying the Width (or MaxWidth) of the StackPanel can make the texts trimmed. You can also try to use another type of container, like Grid.
Other links with similar issue described:
Silverlight text trimming and wrapping issue
TextTrimming not working
http://forums.silverlight.net/t/58227.aspx/1

WPF: Wrapping multi-part text

Currently I'm using a TextBlock to show a single line with an image.
<TextBlock>
<Image Name="StatusImage" Stretch="Fill" MaxWidth="12" MaxHeight="12"
Source="/Aam.Cerberus.Applications;component/Images/Warning.png"></Image>
<TextBlock Text="{Binding Path=ServiceStatusText}"></TextBlock>
<TextBlock Text=" ("></TextBlock>
<TextBlock Text="{Binding Path=ServiceMachineName}"></TextBlock>
<TextBlock Text=")"></TextBlock>
</TextBlock>
My questions are:
Is a TextBlock the right way to do this sort of thing?
How do I enable word wrapping?
You want the TextWrapping="Wrap" property.
However, according to the MSDN
TextBlock is not optimized for scenarios that need to display more than a few lines of content; for such scenarios, a FlowDocument coupled with an appropriate viewing control is a better choice than TextBlock, in terms of performance.

How can I set the text of a WPF Hyperlink via data binding?

In WPF, I want to create a hyperlink that navigates to the details of an object, and I want the text of the hyperlink to be the name of the object. Right now, I have this:
<TextBlock><Hyperlink Command="local:MyCommands.ViewDetails" CommandParameter="{Binding}">Object Name</Hyperlink></TextBlock>
But I want "Object Name" to be bound to the actual name of the object. I would like to do something like this:
<TextBlock><Hyperlink Command="local:MyCommands.ViewDetails" CommandParameter="{Binding}" Text="{Binding Path=Name}"/></TextBlock>
However, the Hyperlink class does not have a text or content property that is suitable for data binding (that is, a dependency property).
Any ideas?
It looks strange, but it works. We do it in about 20 different places in our app. Hyperlink implicitly constructs a <Run/> if you put text in its "content", but in .NET 3.5 <Run/> won't let you bind to it, so you've got to explicitly use a TextBlock.
<TextBlock>
<Hyperlink Command="local:MyCommands.ViewDetails" CommandParameter="{Binding}">
<TextBlock Text="{Binding Path=Name}"/>
</Hyperlink>
</TextBlock>
Update: Note that as of .NET 4.0 the Run.Text property can now be bound:
<Run Text="{Binding Path=Name}" />
This worked for me in a "Page".
<TextBlock>
<Hyperlink NavigateUri="{Binding Path}">
<TextBlock Text="{Binding Path=Path}" />
</Hyperlink>
</TextBlock>
On Windows Store app (and Windows Phone 8.1 RT app) above example does not work, use HyperlinkButton and bind Content and NavigateUri properties as ususal.

Resources