How can I format text in a TextBlock? - c#

I am trying to make a single block of text composed of multiple TextBlocks
- THE TARGET
For example, I want to achieve like the line below:
"I read this line in a ListBox, notice the multiple text formatting"
The way I'm trying to do is
<StcakPanel Orientation="Horizontal" Width="400" >
<TextBlock Text="I read this line in a " TextWrapping="Wrap" />
<TextBlock Text="ListBox" FontStyle="Italic" TextWrapping="Wrap"/>
<TextBlock Text=", notice the multiple " TextWrapping="Wrap" />
<TextBlock Text="text formatting" FontWeight="Bold" TextWrapping="Wrap"/>
<StcakPanel>
- THE PROBLEM
The text does not fit in the StackPanel, despite of setting TextWrapping for TextBlocks and Width for StackPanel.
I want to generate this code at runtime. I don't know how many words do I need to format.
Kind of showing SearchResults with highlighted search keywords.
- THE QUESTION
How can the above target be achieved either using StackPanel or something else? Having the following constraints.
The text length is unknown
The number of textblocks in the stackpanel is unknown
width and height is Unknown
Thanks very much

You should use a single <TextBlock> which can contain multiple <Run>s that can each have their own formatting. If you want to insert a linebreak, you can use the <Linebreak /> control.
<StackPanel Orientation="Horizontal" Width="400" >
<TextBlock>
<Run Text="I read this line in a" />
<Run Text="ListBox" FontStyle="Italic" />
<Run Text=", notice the multiple" />
<Run Text="text formatting" FontWeight="Bold" />
</TextBlock>
<StackPanel>
At that point you probably don't even need the <StackPanel> unless you are going to have multiple <TextBlocks> stacked on top of one another.
See this post for more information and examples: http://www.danderson.me/posts/working-with-the-wpf-textblock-control/
To databind multiple runs within a TextBlock, see this answer: Databinding TextBlock Runs in Silverlight / WP7

Related

Align right in Run control inside TextBlock in WPF

How can I do this with my current implementation ? My last option would be to use a StackPanel as it would involve changing a lot of other things . Any other workarounds would be appreciated.Image below .Incase it is not clear from the image :
In one straight line,
Extreme Left aligned : Apparels (count of defects)
Extreme Right aligned : Nearby outlets ( Count of nearby outlets)
Things I tried :
The flowDirection Property in RUn : did not work
Properties like alignment/orientation that are generally there for other controls don't seem to be there for Run.
<TextBlock
Grid.Column="0"
TextTrimming="CharacterEllipsis"
VerticalAlignment="Center">
<Run Text="{Binding Mode=OneWay, Converter={StaticResource Converter1}}" />
<Run Text="{Binding Defects, Converter={StaticResource Converter2}, Mode=OneWay}" />
<Run Text="{Binding Outlets, Converter={StaticResource Converter3}}" />
</TextBlock>

Displaying numbers raised to the power of (as superscript)

Could anyone please let me know how I can display numbers like 100 to 10², with base 10 raised to the power of "n". The base-10 numbers are dynamic, so I cannot rely on the ASCII coding.
This is for a C# based WPF application.
You can use the Superscript value on the Run.BaselineAlignment property:
<TextBlock FontSize="14">
<Run Text="25" /><Run Text="2" FontSize="8" BaselineAlignment="Superscript" />
</TextBlock>
You can also data bind to the Run.Text property if you prefer:
<TextBlock FontSize="14">
<Run Text="{Binding Number}" /><Run Text="{Binding Power}" FontSize="8"
BaselineAlignment="Superscript" />
</TextBlock>
Also using this method, you can set the various properties individually on the number and the power, so you can get the exact look that you want.

C#, XAML, Metro - How to make some word bigger than others in the same textblock/textbox

I'm trying to make a metro style news app and i want to make the first couple word in the news content bigger than the rest...
Please...is there anyway to do that...?
I'm thanking you in advance =]
You can do this for TextBlock:
<TextBlock>
<TextBlock.Inlines>
<Run FontWeight="Bold" FontSize="14" Text="Big and bold text " />
<Run FontSize="10" Foreground="Red" Text="next to small red text. " />
</TextBlock.Inlines>
</TextBlock>
However, TextBox doesn't support inlines, only a single text value, so this is not supported within a TextBox.

Silverlight Tooltip Remain Visible With Mouseover

Hello and thanks for the help. I have a Treeview that I am populating with a Hierarchical data template, and currently the bottom nodes have a tooltip that generates a small stack panel that is populated with data specific to the item the mouse hovers over. I also have a button sitting in the tooltip, however, as the tooltip does not persist when the mouse moves over it, I am unable to make use of the button like I need to. My xaml looks like this:
<!--=========================== Hierarchical Data template for tree view -->
<!--template for bottom nodes-->
<sdk:HierarchicalDataTemplate x:Key="ModTemplate" ItemsSource="{Binding ApplicationModules}">
<StackPanel Orientation="Horizontal" > <!--======tooltip style to handle format for callout window============-->
<ToolTipService.ToolTip>
<ToolTip HorizontalOffset="0" VerticalOffset="0" Style="{StaticResource ModuleToolTipStyle}">
<StackPanel Width="150" Height="auto" >
<TextBlock Text="Module Info" FontWeight="Bold" TextAlignment="Center"/>
<TextBlock Text="Module State:" FontWeight="Bold" />
<TextBlock Text="{Binding Path=ModInfo.ModuleState}" />
<TextBlock Text="Module Start Time:" FontWeight="Bold" />
<TextBlock Text="{Binding Path=ModInfo.ModuleStartTime}"/>
<TextBlock Text="Module Down Time:" FontWeight="Bold"/>
<TextBlock Text="{Binding Path=ModInfo.ModuleDownTime}" />
<Button Content="More Info" Width="75"></Button>
</StackPanel>
</ToolTip>
</ToolTipService.ToolTip>
<!--============end tooltip style for callout window===================-->
<ContentPresenter Margin="0 0 4 0" Content="{Binding Icon}" />
<TextBlock FontStyle="Italic" Text="{Binding Path=ModuleName}" />
</StackPanel>
</sdk:HierarchicalDataTemplate>
I would like the tooltip to persist when the mouse moves over it so that I can wire an event to the button. How can I achieve this? Thanks again for the help.
You have a couple options to accomplish your goal that I'm aware of. You can go check out the Silverlight Advanced Tooltips project over on codeplex which does what you want (though I personally have not used it so can't give any kind of review.)
Or you can make your own with some creativity. If it were me I would probably skip all that mess, forget the ToolTipService all together and just make my own to dress it up since to a user, what you're providing isn't what they're used to in terms of a tooltip expectation anyway and have cross more over to a callout or popout functionality. I can make an example as soon as I get some freed up time if option #1 doesn't work for you but I hope it does. Essentially both my way, and that project link I provided would do the same thing, which is provide a delay after the MouseLeave event of what it's attached to so the user can get to it before it disappears. Then hand off its visibility condition to that object. Let me know if this doesnt work and I can give you an alternative example using nothing but XAML.

How to wrap multiple textbox wrap together?

I want to 1. from the following image:
My code is here:
<WrapPanel>
<TextBlock Text="Title: " Style="{StaticResource Title}" TextWrapping="Wrap" />
<TextBlock Text="{Binding Description" Style="{StaticResource Normal}" TextWrapping="Wrap" />
</WrapPanel>
But if Description text is short, shown like 2., if Description text is long, shown like 3.
How to do this like 1.?
I have solve my question using Run:
<TextBlock TextWrapping="Wrap">
<Run Text="Title: " Style="{StaticResource TitleRun}"/>
<Run Text="{Binding Description,Mode=OneWay}" Style="{StaticResource NormalRun}"/>
</TextBlock>
Just keep adding them to the Grid with 2 columns and n number of rows, adding new rows/colls as you add them.
You can create a behaviour for that.

Categories