Wrap text nicely in horizontal stackpanel or wrappanel? - xaml

I've seen a few other posts about this but haven't seen anything that solves my problem. Basically, I need to have some text (bold and unbold) wrap within a Stackpanel or Wrappanel nicely. Here is a visual:
I need a combination of the two TextBlocks that you see. I've got the first bold TextBlock which contains "Title: " and then on the same line I need the next TextBlock which may or may not wrap. If it does wrap, I need the top line to stay on the top line and then wrap, as if it were all one TextBlock. Here's a made-in-Paint visual of what I need:
Here's my code that I've got so far:
<toolkit:WrapPanel Orientation="Horizontal">
<TextBlock Text="Title: " FontWeight="Bold"/>
<TextBlock TextWrapping="Wrap" Text="More text goes here " />
</toolkit:WrapPanel>
<toolkit:WrapPanel Orientation="Horizontal">
<TextBlock Text="Title: " FontWeight="Bold"/>
<TextBlock TextWrapping="Wrap" Text="More text goes here and I want it to wrap lines and go underneath the title but I can't get it to to do that. :( " />
</toolkit:WrapPanel>
Now, I'm not opposed to this being done in one TextBlock either (if possible). I know that with TextBlock.Inlines I can add a Run of bold text and then another of regular text. The problem with that is Inlines cannot be bound using MVVM (which I'm not using in this demonstration, but will be using in the final code).
So whatever the solution is, I need to be able to set the values of the TextBlocks from the code-behind so that I know it can be done with MVVM.
Does anyone know of a way to achieve this?

Use the RichTextBox control with a different run for every font style.
<RichTextBox>
<Paragraph>
<Run FontWeight="Bold">Title:</Run>
<Run>More text goes here and I want it to wrap lines and go underneath the title but I can't get it to to do that. :(</Run>
</Paragraph>
</RichTextBox>
You can bind the Text property of the Run elements to your data context, ex.:
<RichTextBox>
<Paragraph>
<Run FontWeight="Bold"
Text="{Binding Header}"></Run>
<Run Text="{Binding Text}"></Run>
</Paragraph>
</RichTextBox>

Fix Width Property of both StackPanel. this may help you. Problem is if you don't set Width Property it consider as Auto width.
<StackPanel Orientation="Horizontal" Width="400">
<TextBlock Text="Title: " FontWeight="Bold"/>
<TextBlock TextWrapping="Wrap" Text="More text goes here " />
</StackPanel >
<StackPanel Orientation="Horizontal" Width="400">
<TextBlock Text="Title: " FontWeight="Bold"/>
<TextBlock TextWrapping="Wrap" Text="More text goes here and I want it to wrap lines and go underneath the title but I can't get it to to do that. :( " />
</StackPanel >

This would helpful for you:
<toolkit:WrapPanel Orientation="Horizontal">
<TextBlock>
<Run FontWeight="Bold" Text="{Binding Header}"></Run>
<Run Text="{Binding Text}"></Run>
</TextBlock>
</toolkit:WrapPanel>

Have you tried using construction like this?
<TextBlock>
<TextBlock.Inlines>
<Bold>
<Bold.Inlines>
<Run Text="Title: "/>
</Bold.Inlines>
</Bold>
<Run Text="{Binding Text}"/>
</TextBlock.Inlines>
</TextBlock>
It works like a charm if text-block should be bound to just one block of text.

Related

How to add a button into a textblock in UWP?

I want to add a Button in a Textblock. I mean, Buttons(Objects) among Text.
The code below does not work.
<TextBlock>
<Button />
</TextBlock>
TextBlock seems to support only text, so which control should I use?
You can't display anything but text in a TextBlock. If you want to display a Button in some running text you could for example use a StackPanel:
<StackPanel Orientation="Horizontal">
<TextBlock Text="Some text with a " /><Button Content="Button" VerticalAlignment="Center" /><TextBlock Text="in it." />
</StackPanel>

Border Control in Windows Phone 7, Auto Height & Width not right

I have a ListBox with this template in it.
<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Key="Template">
<StackPanel Margin="0,10">
<Border BorderBrush="Black" BorderThickness="1" Background="#FFFFC000" Width="460" MinHeight="76">
<StackPanel Margin="4,4,-4,-153">
<TextBlock Text="{Binding }" HorizontalAlignment="Center" VerticalAlignment="Top" Foreground="Black" TextWrapping="Wrap"/>
<TextBlock " Text="{Binding Mode=OneWay}" HorizontalAlignment="Center" VerticalAlignment="Top" Foreground="Black" TextWrapping="Wrap"/>
</StackPanel>
</Border>
</StackPanel>
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
Yet I have to force set a Width and Height on the Border otherwise it makes like Width and Height of "2".
It is like it does not understand I have 2 TextBlock inside it and won't expand to fill both of them.
This leaves me with having to put a fixed height and width in what I don't like as if the text is too big it gets cutoff.
You can toss out your StackPanel's because you don't need them, they're also what's keeping your wrapping from working, you need a panel like a Grid for that. The negative Margin's also isn't something you'd normally see in a DataTemplate for a ListBox so I'd guess you have some other funky layout stuff going on from that sort of practice elsewhere up the tree.
<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Key="Template">
<Grid Margin="0,10">
<Border BorderBrush="Black" BorderThickness="1" Background="#FFFFC000"/>
<TextBlock TextWrapping="Wrap"
HorizontalAlignment="Center" Margin="4,4,-4,-153">
<Run Text="{Binding }"/><LineBreak/>
<Run Text="{Binding Mode=OneWay}"/>
</TextBlock>
</Grid>
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
So unless I'm missing something somewhere this should fix you up as the Grid will handle the sizes of its children for you and consume the space necessary in its parent. However if there's something else in your structure pushing stuff around and won't let it consume that space it should invoke your Wrapping.
Hope this helps :)

ListBox will only scroll up when put inside ExpanderView

I'm relatively new to XAML and Windows Phone development.
I have a ListBox that display a text block. Everything works fine when I just have the ListBox inside a grid, including the scrolling. I wanted the user to be able to collapse the ListBox if they wanted more space on the page to look at other elements, so I surrounded the ListBox with an ExpanderView. Once I did that though, the ListBox won't scroll down anymore. If I try to scroll down, it acts as if I am trying to scroll up and "squishes" the text vertically. If I try to scroll up, it also "squishes" the text vertically, but that is the expected behavior for scrolling up.
Here is the relevant part of my XAML code:
<toolkit:ExpanderView Header="Chatlog" x:Name="chatlogExpander" FontSize="24" VerticalAlignment="Top" Grid.Row="2">
<toolkit:ExpanderView.Items>
<ListBox ItemsSource="{Binding chatlog}" ScrollViewer.VerticalScrollBarVisibility="Auto" Grid.Row="2">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock x:Name="ChatBlock" TextWrapping="Wrap">
<Run Text="{Binding player}" Foreground="{Binding color}" FontSize="24" FontWeight="Bold"/><Run Text="{Binding text}" Foreground="{Binding color}" FontSize="24"/>
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</toolkit:ExpanderView.Items>
</toolkit:ExpanderView>
I appreciate any help you guys can give me!
I tried an example with your code and after specifying a Height for the ListBox, it started to scroll fine.

Why does TextBlock trims ending spaces from the text?

Here are my TextBlocks:
<StackPanel Orientation="Horizontal" Margin="0,3,0,0">
<TextBlock Text="6 or more characters, at least one letter and a number, " FontFamily="Segoe UI" Foreground="#000000" FontSize="13"/>
<TextBlock Text="no symbols" FontFamily="Segoe UI" Foreground="#000000" FontSize="13"/>
</StackPanel>
And here is the output (screen shot):
Why does TextBlock trim ending spaces? However, it works fine when there are leading spaces.
It looks like xml:space="preserve" should do the trick (see Preserving Whitespace in XAML) but that doesn't seem to be working in a Windows Store app (it does in WPF).
If you use the non-breaking space character   it does work
<TextBlock Text="6 or more characters, at least one letter and a number,       " ....
I suppose you could try building a converter on the Text property to check for trailing spaces and replace with non-breaking spaces - presuming the truncation that's happening doesn't occur too early.
Solved with <Run /> in a <TextBlock />..
<StackPanel Orientation="Horizontal" Margin="0,3,0,0">
<TextBlock FontFamily="Segoe UI" Foreground="#000000" FontSize="13">
<Run Text="6 or more characters, at least one letter and a number, " />
<Run Text="no symbols" />
</TextBlock>
</StackPanel>
And word wrapping still works
<StackPanel Orientation="Horizontal" Margin="0,3,0,0">
<TextBlock FontFamily="Segoe UI" Foreground="#000000" FontSize="13"
Width="200" TextWrapping="Wrap">
<Run Text="6 or more characters, at least one letter and a number, " />
<Run Text="no symbols" />
</TextBlock>
</StackPanel>
I would easily use Jim's solution (#160;) if wrapping was not an issue.
In your mind please think about how HTML handles and preserves spaces. This is also how XAML handles and preserves spaces. You would think, of course, that inside a TextBlock it would be more literally handled, huh? Well, it is what it is. At least there's a solution.
Try use xml:space="preserve":
<StackPanel Orientation="Horizontal" Margin="0,3,0,0">
<TextBlock xml:space="preserve" Text="6 or more characters, at least one letter and a number, " FontFamily="Segoe UI" Foreground="#000000" FontSize="13"/>
<TextBlock xml:space="preserve" Text="no symbols" FontFamily="Segoe UI" Foreground="#000000" FontSize="13"/>
</StackPanel>
I've found a different solution! The \u+A0 works when you ALSO set the IsTextSelectionEnabled.
I don't know why this would be, and it was a total surprise (I added the field because I just discovered it while also working on my 'Why does my text get trimmed in Universal Apps?' problem).
Also U+205F (medium mathematical space) also works in conjunction with IsTextSelectionEnabled.
RichTextBlock seems to preserve both leading and trailing whitespace (in WP 8.1 WinRT):
<RichTextBlock>
<RichTextBlock.Blocks>
<Paragraph >
<Paragraph.Inlines>
<Run Text="trailing " /><Run Text="bbb" /><Run Text=" leading" />
</Paragraph.Inlines>
</Paragraph>
</RichTextBlock.Blocks>
</RichTextBlock>
But it also seems to add an extra space between the runs in addition to the of the ones you specify.

How to customize the TextBlock inside the LonglistSelector

<toolkit:LongListSelector>
<toolkit:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" FontSize="22" TextWrapping="Wrap" />
<TextBlock Name="Info" FontSize="18" TextWrapping="Wrap" />
</StackPanel>
</DataTemplate>
</toolkit:LongListSelector.ItemTemplate>
</toolkit:LongListSelector>
I have the above LongListSelector which selects the "Name"(Binded in the first Textblock )values from a list. I also wanted to additional text to it,for which i created one more Textblock below that. I couldn't add the text to the second TextBlock like(Info.Text="HI") because it is inside the LonglistSelector
How to give the values to the second Textblock??
Thanks
Do you mean Info is a property on the DataContext of the page and not on the current item where Name is?
If so, you can use a DataContextProxy to get to the data outside of the list item. If not, you'll have to be more clear on what you mean.