首页 > 解决方案 > 在网格下方添加堆栈面板

问题描述

我有一个简单的表格。我想在网格下方添加一个堆栈面板(包含一个按钮)。我添加了我想要实现的图像

我的代码:

<Window x:Class="CardReader.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:CardReader"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid Margin="0,0,3.4,-0.2">
    <Grid.RowDefinitions >
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" MinWidth="146.4" />
        <ColumnDefinition Width="Auto" MinWidth="244.8" />
    </Grid.ColumnDefinitions>

    <Label Grid.Row="0" Grid.Column="0" Content="Name" Margin="0,0,36.2,0.4" />
    <TextBox Grid.Row="0" Grid.Column="1" IsReadOnly="True" Margin="6.8,0,-21.2,0.4"/>


    <Border HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
        BorderBrush="Black" BorderThickness="1"  Grid.Row="2">
        <StackPanel VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
            <Button VerticalAlignment="Bottom" Content="Save" Height="19"/>
        </StackPanel>
    </Border>
</Grid>

我现在得到的结果是,按钮位于第 0 列的第 1 行的网格中。我希望按钮位于底部,并填充所有宽度,就像您在图像上看到的那样

我想要达到的目标 在此处输入图像描述

它现在的样子,这不是我想要的

在此处输入图像描述

标签: wpfwpf-controls

解决方案


<Window x:Class="CardReader.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:CardReader"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="50" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <StackPanel>
        <Border Margin="5">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <TextBlock Text="Name" />
                <TextBox Grid.Column="1" />
            </Grid>
        </Border>
        <Border Margin="5">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <TextBlock Text="Father Name" />
                <TextBox Grid.Column="1" />
            </Grid>
        </Border>
    </StackPanel>

    <Border
        Grid.Row="0"
        Width="1"
        Margin="0,0,15,0"
        HorizontalAlignment="Center"
        Background="Gray" />
    <Button Grid.Row="1" Content="Save" Margin="20 5"/>
</Grid>


推荐阅读