Brady Moritz
Brady Moritz

Reputation: 8903

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.

Upvotes: 2

Views: 971

Answers (2)

AnthonyWJones
AnthonyWJones

Reputation: 189487

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.

Upvotes: 2

Joe McBride
Joe McBride

Reputation: 3777

Try the HorizontalAlignment property.

HorizontalAlignment="Right"

<Grid x:Name="LayoutRoot" Background="Yellow">
    <StackPanel
        Background="AliceBlue"
        Margin="50">
        <TextBlock
            Text="Some text"
            HorizontalAlignment="Right"/>
        <TextBlock
            Text="Some other longer text"
            HorizontalAlignment="Right"/>
    </StackPanel>
</Grid>

Produces this output:

alt text

Upvotes: 0

Related Questions