如何在网格上拉伸按钮,xaml

时间:2013-11-17 14:14:25

标签: xaml stackpanel wpfgrid

我在整个网格上拉伸按钮时遇到问题。

我的代码如下所示:

<Grid x:Name="LayoutRoot" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="2*"/>
        <RowDefinition Height="8*"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <StackPanel Grid.Column="5" Grid.Row="1" Width="Auto" Height="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <Button Content="Search" Background="#FF13D38D" Click="searchButton_Click" FontSize="48"/>
    </StackPanel>
</Grid>

但它对我不起作用,并在整个网格上拉伸按钮,我尝试了maxwidth / maxheight,但它也无法正常工作,任何人都有想法?

1 个答案:

答案 0 :(得分:10)

<强> TL; DR:
移除StackPanel(请参阅Charleh's comment below question)。

问题已解决

<Grid x:Name="LayoutRoot" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="2*"/>
        <RowDefinition Height="8*"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Button Content="Search" Background="#FF13D38D" Click="searchButton_Click" FontSize="48" Grid.Column="5" Grid.Row="1"/>
</Grid>