Xaml - Textblock Alignment - xaml

I'm trying to create a ListViewItemTemplate and my template needs to have two textblocks side by side. The one on the left is a message textblock and should stay left and the one on the right is a date textblock. That textblock needs to stretch to the end of the ItemTemplate and needs to fit all the text. I'm trying to achieve this:
But this is what I get:
The XAML code for this is:
<DataTemplate x:Key="ItemTemplate">
<Grid Height="84">
<StackPanel Holding="ListViewItem_Holding">
<TextBlock Text="{Binding Title}" FontFamily="Segoe WP" FontSize="21" />
<StackPanel Margin="0,0,-3,0" Orientation="Horizontal">
<TextBlock Text="{Binding Post}" FontFamily="Segoe WP SemiLight" FontSize="18" Margin="0,0,-1,0" Height="26" />
<TextBlock Text="{Binding Modified}" FontFamily="Segoe WP SemiLight" FontSize="18" Margin="0,0,-3,0" SelectionChanged="TextBlock_SelectionChanged" />
</StackPanel>
<TextBlock Text="{Binding ID}" FontFamily="Segoe WP SemiLight" FontSize="18" Visibility="Collapsed" />
</StackPanel>
</Grid>
</DataTemplate>
How can I fix this? I need it so that it stretches properly and at the same time fits all the text regardless of the orientation.

I would personally use a Grid as it is far more flexible than StackPanel and deals with different screen sizes much better. something like below
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Text="Cant send: message" FontFamily="Segoe WP SemiLight"
FontSize="18" Margin="0,0,-1,0"
TextWrapping="Wrap"/>
<TextBlock Text="1:14a" Grid.Column="1" FontFamily="Segoe WP SemiLight"
TextWrapping="Wrap" FontSize="18"
HorizontalAlignment="Right"
SelectionChanged="TextBlock_SelectionChanged"
Margin="0,-15,0,0"/>
</Grid>
Note 2 columns are defined with equal widths. You can adjust this to your needs.
Also both TextBlocks have
TextWrapping="Wrap"
which means your text will go to available space (column width) then wrap to the next line if space is available.
The second TextBlock is aligned to the right so adjust margins as appropriate.
HorizontalAlignment="Right"

You can make this with Grid.RowDefinitions and Grid.ColumnDefinitions.
Base on your picture you have data on two columns and one line.
So you can size your screen on two columns and one line. You don't need to use stackpanel to achieve this.
<Grid Height="84">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="User" TextWrapping="Wrap" FontFamily="Segoe WP" FontSize="21" Grid.Column="0"/>
<TextBlock Text="1:41a" FontFamily="Segoe WP SemiLight" FontSize="18" Grid.Column="1" HorizontalAlignment="Right"/>
</Grid>

Related

Center text vertically (TextBlock)

I'm struggling to vertically center text using TextBlock. I know that it adds extra space above the space in case you use accents but why it isn't consistent with the space below then? There's a few extra pixels.
<StackPanel Orientation="Horizontal" VerticalAlignment="Top" HorizontalAlignment="Center" Margin="0,40,0,0" Background="#FF191919" BorderThickness="1" BorderBrush="#FFBF0077">
<Grid Width="41" HorizontalAlignment="Left" Padding="0" BorderThickness="0,0,1,0" BorderBrush="#FFBF0077">
<TextBlock x:Name="TextBlockLocalScore" Text="0" FontFamily="Courier New" Foreground="#FF007AFF" TextAlignment="Center" VerticalAlignment="Center" HorizontalAlignment="Center" />
</Grid>
<Grid Padding="4,8" BorderThickness="8,0" Width="104">
<TextBlock x:Name="TextBlockMinutesLeft" Text="00" HorizontalAlignment="Left" FontSize="36" VerticalAlignment="Center" FontFamily="Segoe UI Light" FontWeight="Light" TextAlignment="Center" Height="27" TextLineBounds="Tight" Margin="0,0,4,0" />
<TextBlock x:Name="TextBlockSecondsLeft" Text="30" HorizontalAlignment="Right" FontSize="36" VerticalAlignment="Center" FontFamily="Segoe UI Light" FontWeight="Light" OpticalMarginAlignment="TrimSideBearings" TextAlignment="Center" Height="27" TextLineBounds="Tight" />
</Grid>
<Grid Width="41" HorizontalAlignment="Right" Padding="0,8" BorderThickness="1,0,0,0" BorderBrush="#FFBF0077">
<TextBlock x:Name="TextBlockRemoteScore" Text="0" HorizontalAlignment="Center" FontSize="36" VerticalAlignment="Center" FontFamily="Segoe UI Light" FontWeight="Light" OpticalMarginAlignment="TrimSideBearings" TextAlignment="Center" Height="27" TextLineBounds="Tight" Foreground="#FFFF3B30" />
</Grid>
</StackPanel>
I've played with Line Height and Text Line Bounds but I can't why a way to make the text still vertical centered once I change the font.
Updated code to reproduce issue:
<StackPanel Orientation="Horizontal" VerticalAlignment="Top" HorizontalAlignment="Center" Margin="0,100,0,0" Background="#FF191919" BorderThickness="1" BorderBrush="#FFBF0077">
<Grid Width="51" HorizontalAlignment="Left" Padding="0" BorderThickness="0,0,1,0" BorderBrush="#FFBF0077">
<TextBlock Text="0" FontFamily="Courier New" FontSize="36" Foreground="#FF007AFF" TextAlignment="Center" VerticalAlignment="Center" HorizontalAlignment="Center" />
</Grid>
<StackPanel Padding="4" BorderThickness="8,0" Orientation="Horizontal">
<TextBlock Text="01" FontFamily="Courier New" FontSize="36" TextAlignment="Left" VerticalAlignment="Center" Margin="0,0,4,0" />
<TextBlock Text="11" FontFamily="Courier New" FontSize="36" TextAlignment="Right" VerticalAlignment="Center" />
</StackPanel>
<Grid Width="51" HorizontalAlignment="Right" Padding="0,8" BorderThickness="1,0,0,0" BorderBrush="#FFBF0077">
<TextBlock Text="0" FontFamily="Courier New" FontSize="36" Foreground="#FF007AFF" TextAlignment="Center" VerticalAlignment="Center" HorizontalAlignment="Center" />
</Grid>
</StackPanel>
You should have a play with these two properties. In your case you are probably more interested in TextLineBounds.
<TextBlock Text="80" FontSize="40" TextLineBounds="Tight" OpticalMarginAlignment="TrimSideBearings" />
Update
I am not sure if your screenshot is 100% accurate. I have used your code to produce the following pictures. Note I have scaled them up by 10 times.
<Grid Width="41" HorizontalAlignment="Left" Padding="0" BorderThickness="0,0,1,0" BorderBrush="#FFBF0077">
<TextBlock x:Name="TextBlockLocalScore" TextLineBounds="Full" Text="80" FontFamily="Courier New" FontSize="36" Foreground="#FF007AFF" TextAlignment="Center" VerticalAlignment="Center" HorizontalAlignment="Center" />
<Rectangle UseLayoutRounding="False" HorizontalAlignment="Center" Fill="White" Height="10" VerticalAlignment="Top" Width="1" Margin="-21,0,0,0" />
<Rectangle UseLayoutRounding="False" HorizontalAlignment="Center" Fill="White" Height="10" VerticalAlignment="Bottom" Width="1" Margin="-21,0,0,0" />
</Grid>
With TextLineBounds set to Tight
With TextLineBounds set to Full
Yes, on the first one, there's still a tiny little gap (about 0.3 epx) between the bottom edge of number 8 and the Line, but this is mostly due to layout rounding and effective pixel snapping. I don't think it's noticeable to human eyes. :)
P.S. You cannot purely rely on checking the visuals from Blend Designer as sometimes the artboard doesn't get updated on time to give you the correct result. You should always run your app and check from there.
I have tested your code and reproduced this behavior. If your have not set the height and width of TextBlock, it will set them based on your text font size automatically .
For example, If the FontFamily is Courier New and the font size is 25, and the actual width and height of TextBlock is 16.00244140625 , 29.3203125. It will generate deviation when you center the text vertically. However, you could manual correct it via modifying the Padding just like the follow

Windows 10 xaml Controls Centre Screen

I'm working on an app for windows 10. I've previously worked on a handful of Windows 7 applications and I'm trying to get use to the many differences now. I'm trying my luck at a universal app, and I'm wanting to centre a handful of controls so that no matter the screen/windows size the login details are centred.
I've been searching for help with this for a while, but I've found a lot of this to be in its' infancy; in other words there's not a lot of places I'm finding relevant information, never mind helpful. I was hoping someone on here could point me in the right direction?
Here's the window on the desktop view:
Here's what I'm meaning, the textboxes, labels ect don't move with the resize of the window, let alone different screen sizes.
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" FlyoutBase.AttachedFlyout="{StaticResource FlyoutBase1}">
<CheckBox x:Name="chckRemember" Content="Remember" HorizontalAlignment="Left" Margin="1038,441,0,0" VerticalAlignment="Top" ClickMode="Press"/>
<TextBox x:Name="txtUserName" HorizontalAlignment="Left" Margin="818,441,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="202"/>
<PasswordBox x:Name="txtPassword" HorizontalAlignment="Left" Margin="818,478,0,0" VerticalAlignment="Top" Width="202"/>
<TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin="723,446,0,0" TextWrapping="Wrap" Text="User Name:" VerticalAlignment="Top"/>
<TextBlock x:Name="textBlock_Copy" HorizontalAlignment="Left" Margin="735,478,0,0" TextWrapping="Wrap" Text="Password:" VerticalAlignment="Top"/>
<Button x:Name="btnSignin" Content="Login" HorizontalAlignment="Left" Margin="959,539,0,0" VerticalAlignment="Top" Width="61"/>
<Button x:Name="btnCreatAccount" Content="Create Account" HorizontalAlignment="Left" Margin="818,539,0,0" VerticalAlignment="Top" Width="116"/>
<ProgressRing x:Name="progressring1" HorizontalAlignment="Left" Margin="873,592,0,0" VerticalAlignment="Top" Height="87" Width="103"/>
<TextBlock x:Name="txtStatus" HorizontalAlignment="Left" Margin="818,510,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" FontSize="9.333"/>
<Grid HorizontalAlignment="Left" Height="180" Margin="692,412,0,0" VerticalAlignment="Top" Width="496"/>
</Grid>
Don't use Margins for positioning. Use margins only to enforce margins around the object.
To center an element in its parent set its HorizontalAlignment or VerticalAlignment to Center.
For more control, use layout controls such as Grid, StackPanel, and RelativePanel to position the controls where you want. For your layout you could set up 3 Rows and 3 Columns in your Grid to place the controls overall and then set the HorizontalAlignment to place the controls in the grid. Here's a quick update to your Xaml that will keep things centered so long as the window is wide enough for everything to fit (you can use adaptive techniques to reflow things for narrower views)
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" >
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="240"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<CheckBox Grid.Column="2" Grid.Row="1" Margin="10,0" x:Name="chckRemember" Content="Remember" HorizontalAlignment="Left" VerticalAlignment="Top" ClickMode="Press"/>
<TextBox Grid.Column="1" Grid.Row="1" x:Name="txtUserName" HorizontalAlignment="Stretch" TextWrapping="Wrap" Text="" VerticalAlignment="Top" />
<PasswordBox Grid.Column="1" Grid.Row="2" x:Name="txtPassword" HorizontalAlignment="Stretch" VerticalAlignment="Top" />
<TextBlock x:Name="textBlock" Margin="10,0" Grid.Row="1" Grid.Column="0" HorizontalAlignment="Right" TextWrapping="Wrap" Text="User Name:" VerticalAlignment="Center"/>
<TextBlock x:Name="textBlock_Copy" Margin="10,0" Grid.Row="2" Grid.Column="0" HorizontalAlignment="Right" TextWrapping="Wrap" Text="Password:" VerticalAlignment="Center"/>
<Button Grid.Column="1" Grid.Row="3" x:Name="btnSignin" Content="Login" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<Button Grid.Column="1" Grid.Row="3" x:Name="btnCreatAccount" Content="Create Account" HorizontalAlignment="Right" VerticalAlignment="Top" />
<ProgressRing x:Name="progressring1" HorizontalAlignment="Left" VerticalAlignment="Top" Height="87" Width="103"/>
<TextBlock x:Name="txtStatus" HorizontalAlignment="Left" TextWrapping="Wrap" Text="" VerticalAlignment="Top" FontSize="9.333"/>
<Grid HorizontalAlignment="Left" Height="180" VerticalAlignment="Top" Width="496"/>
</Grid>
See Quickstart: Defining layouts and Adding layout controls on MSDN.
The above is a the right approach. Using a Grid Layout with auto as the row height or column width is a good approach because they automatically will scale with you. Depending on the difficulty of your project you can also use Adaptive Triggers. Here is a really intro example to using Adaptive Triggers, http://jamesqquick.com/windows-10-adaptive-triggers-intro/ . This way you can, for instance, make text bigger depending on the screen size.
You're doing good by testing all of the different sizes. This is important! I usually just run as a Windows 10 app and resize it in all directions!

wp8 listbox scrolling doesn't work

I have list box in my WP8 and I can't scroll to down.When I try to scroll down the whole list moves to down .I dont know how to explain it but it does what it shouldn't do. it is like i drag list down.i just want to scroll
here is my xaml code
<Grid x:Name="columngrid" >
<ListBox Name="URLListBox" >
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Background="Transparent" Margin="0,0,0,3" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="170"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"></ColumnDefinition>
<ColumnDefinition Width="100"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock x:Name="surename" Grid.Column="0" Text="{Binding text}" TextWrapping="Wrap" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<Button Tag="{Binding b1Tag}" Grid.Column="1" Content="download" Height="72" Name="button1" Width="100" FontSize="15" Click="addButton_Click"/>
<Button Tag="{Binding b1Tag}" Grid.Column="2" Content="play" Height="72" Name="play_Click" Width="100" FontSize="15" Click="play_Click"/>
<Button Tag="{Binding b1Tag}" Grid.Column="3" Content="pause" Height="72" Name="pause_Click" Width="100" FontSize="15" Click="pause_Click"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
I think the problem is that your listbox is sizing to the content.
When this happens it wont scroll when you attempt to scroll it. It will follow your finger slightly, then bounce back to the top.
In reality what is happening is the listbox is the same size as all the content so it thinks you're at the bottom of the list.
The way to fix this is to manually set the size of the listbox to be the dimensions of the screen.
Something like Margin="0,0,0,0" width="480" height="800"

LongListSelector adjust based on screen width

I am new to windows phone 8 development and i have a very basic question i believe.
I wish to make my LongListSelector have 100% width and height but all the things i have tried didnt work.
I have tried Auto, * etc but nothing seems to do it.
Here is my code:
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="{Binding Path=LocalizedResources.ApplicationTitle, Source={StaticResource LocalizedStrings}}" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock TextWrapping="Wrap" Text="{Binding Path=LocalizedResources.SetupsPageTitle, Source={StaticResource LocalizedStrings}}" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<phone:LongListSelector HorizontalAlignment="Left" Width="400" Height="400"
VerticalAlignment="Top"
Name="lstSetups" ItemsSource="{Binding BusRouteSetups}"
SelectionChanged="lstSetups_SelectionChanged"
LayoutMode="List">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" Margin="0,10,0,10" Background="Coral">
<TextBlock TextWrapping="Wrap" Margin="5" FontWeight="Bold" Text="{Binding Details.Title}" />
<TextBlock TextWrapping="Wrap" Margin="5" Text="{Binding Details.Description}" />
</StackPanel>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
</Grid>
</Grid>
Any help will be much appreciated.
Use HorizontalAlignment = Stretch (also Vertical), then Width and Height of LLS will be set to maximum space that is available to that Control:
<phone:LongListSelector HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Name="lstSetups" ItemsSource="{Binding BusRouteSetups}"
SelectionChanged="lstSetups_SelectionChanged"
LayoutMode="List">
Note that it's maximum height will be determined by Grid (parent of Control). While you have set RowDefinition's Height to * - it means that it will use all the remaining height of the screen (remaining - some space is used by first row (set to auto) - StackPanel with title).
HorizontalAlignment="Left" Width="400" Height="400" VerticalAlignment="Top"
Just remove this part of your code and then the control should be sretched automatically.

WP8 TextBlock Height

I have a handful of TextBlock's on my MainPage.xaml which have a subtle amount of distance between each other to look uniform. The last textbox however is larger and allows the text to be wrapped. The problem which I am facing is when I populate the textbox with a lot of content and all of the textblock's seem to stick to one another.
Image to explain:
The left image is fine, but as you can see on the right image, when I fill the box completely then all the textblock's magically stick together somehow.
Markup:
<ScrollViewer x:Name="LayoutRoot" Background="Transparent">
<Grid>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel Grid.Row="0" Margin="12,17,0,635">
<TextBlock Text="Project" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock Text="Project" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<Grid x:Name="ContentPanel" Margin="12,161,12,0">
<toolkit:ListPicker x:Name="myObj" ItemsSource="{Binding myobj, ElementName=this, Mode=OneTime}" Margin="12,12,12,537" >
<toolkit:ListPicker.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name, Mode=OneTime}" FontSize="{StaticResource PhoneFontSizeSmall}"/>
<TextBlock Text="{Binding Id, Mode=OneTime}" FontSize="{StaticResource PhoneFontSizeSmall}" Visibility="Collapsed"/>
</StackPanel>
</DataTemplate>
</toolkit:ListPicker.ItemTemplate>
<toolkit:ListPicker.FullModeItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name, Mode=OneTime}" Padding="5" FontSize="{StaticResource PhoneFontSizeLarge}"/>
<TextBlock Text="{Binding Id, Mode=OneTime}" FontSize="{StaticResource PhoneFontSizeSmall}" Visibility="Collapsed"/>
</StackPanel>
</DataTemplate>
</toolkit:ListPicker.FullModeItemTemplate>
</toolkit:ListPicker>
<toolkit:PhoneTextBox Height="auto" Name="txtName" Margin="0,70,0,456" Hint="Name" Text="{Binding Name, Mode=TwoWay}" LostFocus="TxtName_OnLostFocus"/>
<toolkit:PhoneTextBox Height="auto" Name="txtAddress" Margin="0,138,0,388" Hint="First Line of Address" Text="{Binding Address, Mode=TwoWay}" LostFocus="TxtAddress_OnLostFocus"/>
<toolkit:PhoneTextBox Height="auto" Name="txtEmail" Margin="0,206,0,320" Hint="Email Address" Text="{Binding Email, Mode=TwoWay}" LostFocus="TxtEmail_OnLostFocus"/>
<toolkit:PhoneTextBox Height="auto" Name="txtTelephone" Margin="0,274,0,252" Hint="Telephone" Text="{Binding Telephone, Mode=TwoWay}" LostFocus="TxtTelephone_OnLostFocus"/>
<toolkit:PhoneTextBox Height="auto" Name="txtComments" Margin="0,340,0,71" Hint="Comments" TextWrapping="Wrap"/>
</Grid>
</Grid>
</ScrollViewer>
Anyone see the obvious problem?
I think problem is with your margin. And the XAML is not efficient also.
Why dont you just add Rows to your Grid and place each TextBox into its own row - that will help you not to HARDCODE margins which lead to messing up the result...
Or the other way is to place everuthing into a Vertical Stack Panel
I fixed the issue by replacing Height="auto" to Height="80" on the TextBlocks.