Search code examples
.netwpfsilverlightxamltextblock

xaml - anchor the left of a textblock to the right of the container


if i have a textblock which i want to vary in width by the size of its contents, but i want the left of the textblock to maintain a certain spacing from the right of the container... how is this accomplished? I feel I'm overlooking something simple. Im specifically using WPF, if that matters.


Solution

  • The solution would be to place the TextBlock inside a StackPanel like this:-

    <StackPanel HorizontalAlighment="Right" Width="200">
       <TextBlock Text="Some Text or binding" />
    </StackPanel>
    

    Where Width 200 represents the fixed distance you want the left border of the TextBlock to be away from the right border of the element containing the StackPanel.