将按钮对接在底部

时间:2014-08-16 06:51:18

标签: wpf wpf-controls

我试图将2个按钮停靠在窗口的底部,这样当我调整窗口大小时它们总是在那里。显然我做错了,因为它不起作用。这是我的代码。我还从示例中看到,有些人在控件上使用DockPanel.Dock而不是容器本身。我出于某种原因不能这样做。在按钮上使用DockPanel.dock会出错。

我的问题是:如何让按钮(或stackPanel)停靠在底部?

<Window x:Class="MeditCal.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<DockPanel LastChildFill="True">
    <StackPanel DockPanel.Dock="Top">      
        <Grid>                              
            <Grid.RowDefinitions>
                <RowDefinition Height="auto" />
                <RowDefinition Height="auto" />
                <RowDefinition Height="auto" />
                <RowDefinition Height="auto" />
                <RowDefinition Height="auto" />                                       
            </Grid.RowDefinitions>                

           <Label Grid.Row="0">Date</Label>
           <TextBox Grid.Row="1" Name="DateTxtBox" Background="WhiteSmoke"/>
            <Label Grid.Row="2">Note</Label>
           <TextBox Grid.Row="3" Name="noteTxtBox" 
                    Background="WhiteSmoke"></TextBox>               
        </Grid>
        <Popup Height="100" Width="100" Name="popUpWin" StaysOpen="false" 
               AllowsTransparency="True" 
               HorizontalAlignment="Center" PopupAnimation="Fade">
            <Border BorderThickness="1" Background="AliceBlue">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="Record added" />
                </StackPanel>
            </Border>
        </Popup>
        <ListView Name="msgArea" Background="WhiteSmoke" MinHeight="150" 
                  Height="138"  />            
    </StackPanel>        

    <DockPanel LastChildFill="True">
        <StackPanel DockPanel.Dock="Bottom">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto" />
                </Grid.RowDefinitions>

                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>

                <Button Name="addButton" Content="Add" Grid.Column="0" 
                        Grid.Row="1" />
                <Button Name="getRecordsButton" Content="Get records" 
                        Grid.Column="1" />
            </Grid>

        </StackPanel>
    </DockPanel>
</DockPanel>

1 个答案:

答案 0 :(得分:0)

目前还不清楚您尝试实现的是哪种布局。我建议您删除内部DockPanel及其StackPanel子项,因为每个孩子只包含一个孩子,这表明您不需要面板包装器。

这样的内容会使Grid包含按钮放在底部,StackPanel填充DockPanel中的剩余空间:

<DockPanel LastChildFill="True">
    <Grid DockPanel.Dock="Bottom">
        <Grid.RowDefinitions>
            <RowDefinition Height="auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Button Name="addButton" Content="Add" Grid.Column="0" Grid.Row="1" />
        <Button Name="getRecordsButton" Content="Get records" Grid.Column="1" />
    </Grid>
    <StackPanel>      
        <Grid>                              
            <Grid.RowDefinitions>
                <RowDefinition Height="auto" />
                <RowDefinition Height="auto" />
                <RowDefinition Height="auto" />
                <RowDefinition Height="auto" />
                <RowDefinition Height="auto" />                                       
            </Grid.RowDefinitions>                

           <Label Grid.Row="0">Date</Label>
           <TextBox Grid.Row="1" Name="DateTxtBox" Background="WhiteSmoke"/>
            <Label Grid.Row="2">Note</Label>
           <TextBox Grid.Row="3" Name="noteTxtBox" Background="WhiteSmoke"></TextBox>               
        </Grid>
        <Popup Height="100" Width="100" Name="popUpWin" StaysOpen="false" AllowsTransparency="True" 
               HorizontalAlignment="Center" PopupAnimation="Fade">
            <Border BorderThickness="1" Background="AliceBlue">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="Record added" />
                </StackPanel>
            </Border>
        </Popup>
        <ListView Name="msgArea" Background="WhiteSmoke" MinHeight="150" Height="138"  />            
    </StackPanel>
</DockPanel>