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>
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 :)
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.
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.
<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.