tags:

views:

27

answers:

1

I'm new at using the WPF Designer in VS2008 and I know very little about it right now. I'm trying to learn via experimentation.

Below is my XAML (generated mostly by VS):

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="478" />
        <RowDefinition Height="64*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="478" />
        <ColumnDefinition Width="140*" />
    </Grid.ColumnDefinitions>
    <Canvas Name="canvas1" Margin="0" />
    <TextBox Grid.Row="1" Grid.Column="0" Name="textBox1" HorizontalAlignment="Center" Width="120" VerticalAlignment="Top" Margin="0,5" Height="25" />
</Grid>

As you can see, my canvas is on top and my text box is below it. I want the text box to be centered horizontally to the width of my canvas. To achieve this, I've set the width of the column to 478, which is the width I want my canvas to be.

Is this an effective way of doing this? I don't really like the solution myself, it feels like there should be a better way. Keep in mind that the "client rect" of my canvas needs to be 478, which means I'm probably going to have to resize the Canvas at runtime later to make sure it is this size. This particular layout needs to be done in such a way that the textbox will properly re-adjust is horizontal position to remain centered below the canvas if the canvas' size changes.

How would others do this?

+1  A: 

You can wrap both the canvas and the textbox in another container, like a StackPanel. Set the width of the container to 478, then set the canvas and textbox to fill the width of the container.

Jay