Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to center TextBox element inside Grid
    text
    copied!<p>I'm developing WP8 app for my own needs and want it to have small live tile with text. Since small tile cannot display text, I'm generating appropriate image with needed text. Here is the code:</p> <pre><code>WriteableBitmap bmpSmall = new WriteableBitmap(159, 159); var grid = new Grid(); grid.Width = bmpSmall.PixelWidth; grid.Height = bmpSmall.PixelHeight; var background = new Canvas(); background.Width = bmpSmall.PixelWidth; background.Height = bmpSmall.PixelHeight; SolidColorBrush backColor = new SolidColorBrush((Color)Application.Current.Resources["PhoneAccentColor"]); background.Background = backColor; var textBlock = new TextBlock(); textBlock.Text = "qwerty"; textBlock.FontWeight = FontWeights.Bold; textBlock.HorizontalAlignment = HorizontalAlignment.Center; textBlock.VerticalAlignment = VerticalAlignment.Center; textBlock.FontSize = 28; textBlock.Foreground = new SolidColorBrush(Colors.White); grid.Children.Add(textBlock); bmpSmall.Render(background, null); bmpSmall.Render(grid, null); bmpSmall.Invalidate(); using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) { using (IsolatedStorageFileStream imageStream = new IsolatedStorageFileStream("/Shared/ShellContent/smallTile.jpg", System.IO.FileMode.Create, isf)) { bmpSmall.SaveJpeg(imageStream, 159, 159, 0, 100); } } ShellTile tile = ShellTile.ActiveTiles.First(); FlipTileData tileData = new FlipTileData(); tileData.SmallBackgroundImage = new Uri(@"isostore:/Shared/ShellContent/smallTile.jpg", UriKind.Absolute); tile.Update(tileData); </code></pre> <p>And result looks like:</p> <p><img src="https://i.stack.imgur.com/nNEJF.png" alt="enter image description here"></p> <p>As you see, text is aligned to top left corner. The question is "Why"? Since I'd set <code>textBlock.HorizontalAlignment</code> and <code>textBlock.VerticalAlignment</code> - I expect it in the center of the image. For example the following XAML looks like you can expect and like I need:</p> <pre><code>&lt;Grid Width="159" Height="159"&gt; &lt;Grid.Background&gt; &lt;SolidColorBrush Color="{StaticResource PhoneAccentColor}"/&gt; &lt;/Grid.Background&gt; &lt;TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontWeight="Bold" FontSize="28" Foreground="White"&gt;qwerty&lt;/TextBlock&gt; &lt;/Grid&gt; </code></pre> <p>What did I miss? How can I center text?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload