Posts Tagged 'textblock'

Right-align a TextBlock in Silverlight

There is currently no way to right-align a TextBlock UI Element in Silverlight, though you can use this workaround to properly position your TextBlock to make it appear right-aligned:


    <TextBlock Text="My Text" Loaded="TextBlock_Loaded" Width="80"/>

The Loaded event handler

       private void TextBlock_Loaded(object sender, RoutedEventArgs e)
            TextBlock tb = (TextBlock) sender;
            Canvas.SetLeft(tb, Canvas.GetLeft(tb) + tb.Width - tb.ActualWidth);

By wrapping the TextBlock in the Canvas container, you can fix its position easily.