Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relative positioning in WPF xaml

Tags:

wpf

Is it possible to position a control in the XAML definiton relative to another control?
for example if I want to place a control exactly in the middle of another wider control.
and if so - how?

like image 937
Idov Avatar asked Sep 29 '12 14:09

Idov


1 Answers

If you want to center the controls you could wrap them in a grid:

<Grid>
    <TextBox Height="132"   Width="229" VerticalAlignment="Center" HorizontalAlignment="Center" />
    <Button Content="Button" Height="23" Width="75" VerticalAlignment="Center" HorizontalAlignment="Center" />
</Grid>

Other positioning can be accomplished by other panels like StackPanel etc. Grid beeing the most flexible choice.

Edit: updated with grid and columns for controlling position:

<Grid Height="50" Width="200">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="50*" />
        <ColumnDefinition Width="20*" />
        <ColumnDefinition Width="75*" />
    </Grid.ColumnDefinitions>
    <TextBox Grid.Column="0" Background="Red" />
    <Button Content="Button"  Grid.Column="2"   />
</Grid>
like image 135
Johan Larsson Avatar answered Nov 15 '22 05:11

Johan Larsson