Windows Phone 8 - TextBlock in StackPanel not wrapping Text properly? - c#

I have an app that is based on the Panorama control. Clicking on an item on one of the Panorama control's pages (tabs) goes to a details page. On that details page I have the XAML below that renders the item content. As you can see there is a TextBlock hosted by a StackPanel in the phone:PanoramaItem.Header element. I want the text in that element to wrap the video title the TextBlock is bound to in the view model. I have TextWrapping set to wrap and I have assigned it a fixed width so the TextBlock doesn't grow/expand (and therefore won't wrap). The text does wrap, but some lines still appear clipped with some words appearing "cut off". What is wrong with my XAML?
I read about using a DockPanel instead:
TextBlock Wrapping in WPF Layout
But this app has a StackPanel used on a lot of different pages so if I can stick with a StackPanel I'd prefer it.
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid Grid.Row="0" Visibility="{Binding IsInternetAvailable}" Background="{StaticResource PhoneAccentBrush}" Height="30">
<TextBlock Text="{Binding Path=LocalizedResources.NoConnection, Source={StaticResource LocalizedStrings}}" Margin="10, 0"/>
</Grid>
<phone:Panorama x:Name="PanoramaFavorites_DetailVideos" Grid.Row="2" Foreground="{StaticResource CustomApplicationTextBrush}" Background="{StaticResource CustomApplicationBackgroundImage}" SelectionChanged="panoramaFavorites_DetailVideos_SelectionChanged">
<phone:Panorama.Title>
<StackPanel Orientation="Horizontal" Margin="0,15,0,0">
<Image Height="85" Width="85" Source="http://appstudiodata.blob.core.windows.net/apps/1383/db936107-bce6-41a2-9d95-1d342f66c6bb/res/Logo-b3883645-a6cd-4cc8-82be-97c87a266656.png" Stretch="Uniform" VerticalAlignment="Bottom" HorizontalAlignment="Left" Margin="5,0,10,5" RenderTransformOrigin="0.5,0.5" />
<TextBlock FontSize="92" Text="Robot Videos" FontFamily="Segoe WP Light" Foreground="{StaticResource CustomTitleApplicationTextBrush}" VerticalAlignment="Stretch"/>
</StackPanel>
</phone:Panorama.Title>
<phone:PanoramaItem x:Name="PanoramaFavorites_DetailVideos0" Margin="0,0,0,25">
<phone:PanoramaItem.Header>
<StackPanel Orientation="Horizontal" Margin="0,7,0,0">
<TextBlock Text="{Binding CurrentYouTubeVideo.Title, Converter={StaticResource SanitizeString}}" Foreground="{StaticResource CustomApplicationTextBrush}" FontSize="36" TextWrapping="Wrap" Width="440" Height="170"/>
</StackPanel>
</phone:PanoramaItem.Header>
<ctl:FlipControl NextElementCommand="{Binding NextpanoramaFavorites_DetailVideos0}" PreviousElementCommand="{Binding PreviouspanoramaFavorites_DetailVideos0}" ShowPreviousButton="{Binding HasPreviouspanoramaFavorites_DetailVideos0}" ShowNextButton="{Binding HasNextpanoramaFavorites_DetailVideos0}">
<ctl:FlipControl.InnerContent>
<Grid Margin="10,5,5,5">
<ScrollViewer>
<ctl:YouTubePlayer Margin="0,10" MaxHeight="250" VerticalAlignment="Top" VideoId="{Binding CurrentYouTubeVideo.VideoId, TargetNullValue={StaticResource DefaultNoImageValue}}"/>
</ScrollViewer>
</Grid>
</ctl:FlipControl.InnerContent>
</ctl:FlipControl>
</phone:PanoramaItem>
</phone:Panorama>
</Grid>

As per your comments, this works for me:
<StackPanel Orientation="Vertical" Margin="0,7,0,0">
<TextBlock Text="{Binding CurrentYouTubeVideo.Title, Converter={StaticResource SanitizeString}}"
Foreground="{StaticResource CustomApplicationTextBrush}"
FontSize="36"
TextWrapping="Wrap"
MaxWidth="440"/>
</StackPanel>

Related

How to add a separator to the Expander component in UWP app?

I'm looking for a way of implementing this kind of separator to the expander component in UWP app:
Any ideas how to do that?
I've managed to create the similar look of the expander, but I don't know how to implement separators?
Also, I would like to learn how to make expanders kind of "push" other components down when I expand them, so they don't overlap with other components, but simply move them.
Here's the XAML code for this expander:
<muxc:Expander x:Name="FontExpander"
x:Uid="FontExpander"
Width="1155"
IsExpanded="True"
Margin="22,323,0,0"
VerticalAlignment="Top"
Grid.Row="2"
Height="380"
HorizontalContentAlignment="Left"
ExpandDirection="Down">
<muxc:Expander.Header>
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Center">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<FontIcon Margin="0,0,12,0"
VerticalAlignment="Center"
Foreground="{ThemeResource TextFillColorPrimaryBrush}"
FontSize="16"
Glyph=""/>
<StackPanel Grid.Column="1"
Margin="0,12"
Orientation="Vertical">
<TextBlock x:Uid="SettingsFontTitle" Style="{StaticResource BodyTextBlockStyle}"/>
<TextBlock x:Uid="SettingsFontDescription"
Style="{StaticResource CaptionTextBlockStyle}"
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
TextWrapping="WrapWholeWords"/>
</StackPanel>
</Grid>
</muxc:Expander.Header>
<muxc:Expander.Content>
<Grid Margin="-18,-170,-17,-170">
<TextBlock x:Name="SettingsFamily" x:Uid="SettingsFamily" Text="Family" Margin="50,51,1000,286"/>
<ComboBox x:Name="ComboBoxFamily" ItemsSource="{x:Bind Fonts}" Width="200" Margin="950,41,0,0" SelectionChanged="ComboBoxFamily_SelectionChanged" />
<TextBlock x:Name="SettingsStyle" x:Uid="SettingsStyle" Text="Style" Margin="50,106,995,218" Loaded="SettingsStyle_Loaded"/>
<ComboBox x:Name="ComboBoxStyle" Width="200" Margin="950,101,0,0">
<x:String>Regular</x:String>
</ComboBox>
<TextBlock x:Name="SettingsSize" x:Uid="SettingsSize" Text="Size" Margin="50,166,1002,158"/>
<ComboBox x:Name="ComboBoxSize" ItemsSource="{x:Bind FontSizes}" Width="200" Margin="950,161,0,0"/>
<TextBlock x:Name="Text" x:Uid="SettingsText" Text="Hello World! I am so excited to be here!" Margin="62,224,38,126" TextWrapping="Wrap" FontSize="{x:Bind (x:Double)ComboBoxSize.SelectedValue, Mode=OneWay}" TextAlignment="Center"/>
</Grid>
</muxc:Expander.Content>
</muxc:Expander>
Any help would be much appreciated!
A separate can for example be implemented using a Border or a Grid:
<Grid Background="DarkGray" Height="1" />
As a side note, you shouldn't use margins to position your elements. Use an appropriate layout Panel. Using a Grid, you should add ColumnDefinitions and RowDefinitions to it as examplified here.

Stackpanel: Black background on wp8

I have one problem - my stackpanel is vey big and it breaks(background color is changed to black).. What should i do to improve it?
<Grid x:Name="LayoutRoot" Background="#FF2D2D2D" >
<Grid.RowDefinitions>
<RowDefinition Height="60"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel x:Name="TitlePanel" Orientation="Horizontal" Grid.Row="0">
..
</StackPanel>
<ScrollViewer Name="viewer" Grid.Row="1" >
<StackPanel Name="ContentGrid" Canvas.ZIndex="1" Margin="0, 0,0,80" Background="White" Width="452">
<Image Name="ImageImage" Height="300" VerticalAlignment="Top"/>
<TextBlock Name="DateText" TextWrapping="Wrap" TextAlignment="Center" FontSize="18" Foreground="Black"/>
<TextBlock Name="TitleText" TextWrapping="Wrap" FontSize="20" TextAlignment="Center" Foreground="Black" Margin="10,0"/>
<TextBlock TextWrapping="Wrap" Margin="10,15,10,2" Name="DescroptionText" FontSize="23" Foreground="#FF494949" FontFamily="Portable User Interface"/>
<TextBlock TextWrapping="Wrap" TextTrimming="WordEllipsis" Margin="10,0,10,2" Name="DescroptionText2" FontSize="23" Foreground="#FF494949"/>
<ListBox SelectionChanged="GridImages_SelectionChanged" ScrollViewer.VerticalScrollBarVisibility="Disabled" Name="GridImages" ItemTemplate="{StaticResource AttachmentsItemTemlate}" Grid.RowSpan="2" FontFamily="Portable User Interface" >
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel Orientation="Horizontal" FlowDirection="LeftToRight" ItemWidth="150" ItemHeight="150"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</StackPanel>
</ScrollViewer>
</Grid>
There is a limitation of 2048px in Windows Phone UIElements.
You should try to break your different elements into smaller elements (for example: you could put several stackpanels inside the first one).
In TextBox elements there is a workaround explained here.
Best this is to make the Main Window to white background and Make your stackpanel as transparent background instead of giving any color to it. It works great as I have tested it.
Try this code, but first you need to add windows toolkit library.
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
<toolkit:PhoneTextBox
TextWrapping="Wrap"
Name="txtString"
ScrollViewer.VerticalScrollBarVisibility="Visible"
MaxHeight="200"
PlaceholderText="Add text"
Padding="5"
Width="370"
FontFamily="Open Sans Light"
FontSize="22"
Height="Auto" ///Use Auto or provide height value
Margin="10"
/>
This code is used for Scrolling as well work for Auto Growing Textbox. If you don't want Auto Growing Textbox then provide height instead of Auto in Height attribute.

Find textblock in code to manipulate color

I need to find a specific control in xaml to manipulate in code to change the background.
My problem is, can't find the specific control.
I tried .FindByName(Textblock) and with visualtreehelper. Also tried to type it in code txtVeranderkleur but the system doesn't know the control because it's inside childs i guess. Didn't work all for me.
I need to find "txtVeranderkleur". So I can change the color in code.
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="80"/>
</Grid.RowDefinitions>
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="0,0,0,28" Orientation="Horizontal">
<Border Background="#EE2E24" CornerRadius="15,15,15,15" Width="450" Margin="15,15,15,15">
<TextBlock x:Name="Events" TextWrapping="Wrap" Text="Evenementen" Style="{StaticResource subtitle}" Margin="15,15,15,15"/>
</Border>
</StackPanel>
<ListBox Grid.Row="1" Margin="12,-15,0,12" x:Name="lbDagprogrammaInfo" SelectionChanged="lbDagprogrammaInfo_SelectionChanged" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="15,0,0,17">
<Border Width="70" Height="70" BorderBrush="#EE2E24" Background="#EE2E24" BorderThickness="3" CornerRadius="3,3,3,3" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="0,5,0,0">
<TextBlock Width="70" Height="70" Text="{Binding LineTeller}" Style="{StaticResource contentRect}"></TextBlock>
</Border>
<StackPanel Orientation="Horizontal" Margin="8,0,0,0">
**<TextBlock x:Name="txtVeranderkleur" Style="{StaticResource contentText}">
<Run Text="{Binding LineUur}"></Run>
<Run Text="{Binding LineNaam}"></Run>
</TextBlock>**
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<StackPanel Width="480" Height="80" Background="Black" Grid.Row="2">
<Image x:Name="imgSponsor" Source="{Binding LineSponsorFoto}" Height="80" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="3" />
</StackPanel>
</Grid>
FindName won't work for elements inside of a DataTemplate.
If you must, you can use lbDagprogrammaInfo.ItemContainerGenerator.ContainerFromIndex to get the ListBoxItem containing the txtVeranderkleur you want to modify and use VisualTreeHelper.GetChild to search down the visual tree for the TextBlock.
If you can logically determine the color based on data in the DataContext of each item, you could bind Background to the appropriate Property and use an IValueConverter to choose the color.
You should also look into using Visual States to change the color if you're only looking to change the color based on the ListBox functionality, such as selection.
EDIT:
Here's a snippet of what the VisualTreeHelper path would look like, though you should find a more generic approach.
ListBoxItem l = lbDagprogrammaInfo.ItemContainerGenerator.ContainerFromIndex(0) as ListBoxItem;
Border b = VisualTreeHelper.GetChild(l, 0) as Border;
ContentControl c = VisualTreeHelper.GetChild(b, 0) as ContentControl;
ContentPresenter p = VisualTreeHelper.GetChild(c, 0) as ContentPresenter;
StackPanel s = VisualTreeHelper.GetChild(p, 0) as StackPanel;
TextBlock t = s.FindName("txtVeranderkleur") as TextBlock;

WPF: How can I stretch the middle child in a DockPanel?

I added a DockPanel to a RadioButton element such that I can distribute the radio button label, a textbox and a button horizontally using 100% of the width.
Using LastChildFill="True"within the DockPanel stretches the last element. This works out nicely if the textbox is the last child in the panel. But, as the button is the last element and has a fixed width, the textbox should be stretched. However, there's no such property like 2ndChildFill="True".
My code looks like this:
<RadioButton HorizontalAlignment="Stretch"
HorizontalContentAlignment="Stretch">
<DockPanel >
<TextBlock VerticalAlignment="Center">in location:</TextBlock>
<TextBox Grid.Column="1" Margin="10,0,0,0">Path string</TextBox>
<Button HorizontalAlignment="Right"
Margin="10,0,0,0" Padding="3,0">...</Button>
</DockPanel>
</RadioButton>
And it gives me this:
Any ideas, hints to fix this? Many thanks in advance...
You need to set DockPanel.Dock attached property for your elements and leave TextBox as the last element:
<RadioButton HorizontalAlignment="Stretch"
HorizontalContentAlignment="Stretch">
<DockPanel LastChildFill="True">
<TextBlock DockPanel.Dock="Left"
VerticalAlignment="Center"
Text="in location:" />
<Button DockPanel.Dock="Right"
Margin="10,0,0,0"
Padding="3,0"
Content="..." />
<TextBox Margin="10,0,0,0">
Path string
</TextBox>
</DockPanel>
</RadioButton>
Accepted answer is fixing your problem but creates another one. If someone uses keyboard (TAB) to navigate through your interface, Button will be focused before TextBox. This could be very annoying on a long run. If you don't want to brake your tab orders use Grid instead of DockPanel:
<RadioButton HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" VerticalAlignment="Center">in location:</TextBlock>
<TextBox Grid.Column="1" Margin="10,0,0,0">Path string</TextBox>
<Button Grid.Column="2" HorizontalAlignment="Right" Margin="10,0,0,0" Padding="3,0">...</Button>
</Grid>
</RadioButton>
Alternative would be to control tab orders yourself using TabIndex attribute. This can be tricky though, especially when you display your control in a collection.
<RadioButton HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" TabIndex="0">
<DockPanel LastChildFill="True">
<TextBlock DockPanel.Dock="Left" VerticalAlignment="Center" Text="in location:" />
<Button DockPanel.Dock="Right" Margin="10,0,0,0" Padding="3,0" Content="..." TabIndex="2"/>
<TextBox Margin="10,0,0,0" TabIndex="1">Path string</TextBox>
</DockPanel>
</RadioButton>

listbox's item with dynamic height

I have a listBox, wich I fill from some collection. Each item of collection has some info: static name and picture, and dynamic content(text, photo or photos, etc). I want to change the listbox's item height for each item of collection. Height depends of content, if it has only text - item of listbox must have static (textblock for name, image for picture) and a textblock with text, and textblock's heigth must change acording to text (if it's 2-3 strings height will be min, if text fit in 15-20 strings - height of textblock must change accordind to strings), if item of collection include text and image - it must have static (textblock for name, image for picture), textblock for text, and image for image. Height of listbox's item must depend of textblock size and image size.
At this time, I have a listbox with static item's height, and want to make height dynamic.
here is one of my listbox's item
<local:NewsTemplateSelector.Photo>
<DataTemplate>
<Border
BorderBrush="Red"
BorderThickness="2"
Width="400"
Height="auto"
Margin="10">
<StackPanel Orientation="Horizontal" Width="400" Height="auto">
<Image
Source="{Binding SourceImage}"
Height="75"
Width="75"
Margin="0,-225,0,0" />
<Canvas Width="400">
<TextBlock
Text="{Binding SourceName}"
Foreground="Black"
FontSize="25"
TextWrapping="Wrap"
Height="55"
Width="326"
d:LayoutOverrides="VerticalAlignment, Height" />
<Image
Source="{Binding Photo[0].Big}"
Height="auto"
VerticalAlignment="Bottom"
HorizontalAlignment="Right"
Width="326"
Canvas.Top="69"/>
</Canvas>
</StackPanel>
</Border>
</DataTemplate>
</local:NewsTemplateSelector.Photo>
UPD: Tried
<local:NewsTemplateSelector.Photo>
<DataTemplate>
<Border
BorderBrush="Red"
BorderThickness="2"
Width="auto"
Height="auto"
Margin="10">
<Canvas Width="auto">
<Image
Source="{Binding SourceImage}"
Height="auto"
Width="auto"
HorizontalAlignment="Stretch" />
<TextBlock
HorizontalAlignment="Stretch"
Text="{Binding SourceName}"
Foreground="Black"
FontSize="25"
TextWrapping="Wrap"
Height="auto"
Width="auto"
MaxHeight="300"
MaxWidth="460" />
<TextBlock
HorizontalAlignment="Stretch"
Text="{Binding Texts}"
Foreground="Black"
FontSize="25"
TextWrapping="Wrap"
Height="auto"
Width="auto"
MinHeight="150"
MaxHeight="300"
MaxWidth="460" />
<Image
HorizontalAlignment="Stretch"
Source="{Binding Photo[0].Big}"
Height="auto"
MinHeight="150"
MaxHeight="300"
MaxWidth="460"
Width="auto"/>
</Canvas>
</Border>
</DataTemplate>
</local:NewsTemplateSelector.Photo>`
but photos superimposed on each other, and images with name SourseImage, also superimposed at the top of screen
Try by replacing all the hard coded properties with auto, like height=auto and width=auto.
If you want to fill the total row make the HorizontalAlignment=:stretch".
Make the textblock property TextWrapping="wrap" and MinWidth as fixed as you want.
you can use "auto" value for Height of ListBox in xaml code
<ListBox Height="auto">
</ListBox>

Categories