1

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.

ispiro
  • 26,556
  • 38
  • 136
  • 291
  • Could you add more context to your XAML? The definition of enclosing container? – Steve Sep 02 '15 at 20:08
  • @Steve Nothing special - A Grid with Margin 0. on it a Canvas with Margin 10 (and a background color). And on that - the TextBox. I made sure to remove all else so it doesn't interfere. And, yes, I ran the program, not just trusted the designer. When I resize the Window - the Canvas is resized, but the TextBox not. – ispiro Sep 02 '15 at 20:11
  • Here your answer http://stackoverflow.com/questions/13621041/stretch-items-to-fill-canvas – Steve Sep 02 '15 at 20:27
  • @Steve Thanks. But how would I change `Width="{Binding ActualWidth , ElementName=Canvas1}"` to binding to the `Width - 270`? (Perhaps an elementary question, but I'm just beginning with XAML. I tried simply adding the `- 270` but that didn't work.) – ispiro Sep 02 '15 at 20:33

1 Answers1

2

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>
Justin XL
  • 38,763
  • 7
  • 88
  • 133
  • I'm accepting this (and you definitely deserve the rep for your answer to the other question and comment!) but I'll note that setting the `Margin`s is usually the solution here, except that I didn't know that `Canvas` is special in that it ignores them. – ispiro Sep 25 '15 at 13:13
  • You are absolutely right. Normally you just need to set the `Margin` but I thought you wanted a parent container to wrap it hence the answer. :) – Justin XL Sep 25 '15 at 13:20