WPF xaml中的相对定位

时间:2012-09-29 14:35:31

标签: wpf relativelayout

是否可以将控件定位在XAML定义中相对于另一个控件?
例如,如果我想将控件正好放在另一个更宽的控件的中间 如果是的话 - 如何?

1 个答案:

答案 0 :(得分:6)

如果您想将控件居中,可以将它们包裹在网格中:

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

其他定位可以通过StackPanel等其他面板完成。网格是最灵活的选择。

编辑:使用网格和列进行更新以控制位置:

<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>