Anchor a TextBox to the right and Left (so it’s st

2019-08-15 21:29发布

问题:

I’m looking for the XAML equivalent of Winforms’ Anchor property. I want to anchor a TextBox that’s on a Canvas (on a UWP app) to the left and right so it’s always 260 from the left and 10 from the right. I’ve tried many things, but the one that looks most promising was:

<TextBox Height="Auto" Width="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  TextWrapping="Wrap" Text="TextBox"  Margin="260,10,10,10"/>

It does not, however anchor to the right.

回答1:

I would use a 3-column Grid.

<Grid x:Name="YourOuterGrid">
    <Grid VerticalAlignment="Top">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="160" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="10" />
        </Grid.ColumnDefinitions>
        <TextBox TextWrapping="Wrap" Grid.Column="1" />
    </Grid>
</Grid>