【问题标题】:WPF: Two controls in a DockPanel docked "Top" and "Bottom" still leaves space at the bottomWPF:停靠在“顶部”和“底部”的 DockPanel 中的两个控件仍然在底部留出空间
【发布时间】:2018-01-19 22:02:23
【问题描述】:

我有一个简单的 WPF UserControl,如下所示。

我不明白为什么这段代码没有将绿色网格固定到 DockPanel 的底部:

如果我在两个块之间添加一些东西,那么绿色将固定在底部:

下面是简单的代码:

<UserControl
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:tWorks.Alfa.OperatorClient.UserControls.Vehicles"
         xmlns:dxlc="http://schemas.devexpress.com/winfx/2008/xaml/layoutcontrol" x:Class="tWorks.Alfa.OperatorClient.UserControls.Vehicles.Misc_Vehicles_GpsTrackBarContext"
         mc:Ignorable="d" 
         d:DesignHeight="260" d:DesignWidth="450">
<DockPanel>
    <Grid DockPanel.Dock="Top" Height="50" Background="Red"></Grid>
    <Grid DockPanel.Dock="Bottom" Height="50" Background="Green"></Grid>
    <Grid Height="50" Background="Blue"></Grid>
</DockPanel>
</UserControl>

【问题讨论】:

    标签: wpf dockpanel


    【解决方案1】:

    你需要防止最后一个孩子拉伸:

    <DockPanel LastChildFill="False"/>
    

    否则DockPanel.Dock="Bottom" 将被最后一个孩子忽略,而是放置在整个剩余区域中。在那里它将对齐中心,因为它的Height="50"

    【讨论】:

    • 谢谢!这是一个非常奇怪的行为。那是什么原因,有人可能会想......荒谬=)
    • "Reason" 是个大词...但如果 WPF 开发人员确实允许DockPanel.Dock="Fill",我会很高兴,这样我们就不需要将填充元素放在最后并搞砸默认值制表符排序等等......我不能给出理由,只知道我自己学得很辛苦,所以现在为别人回答相对容易:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-02
    • 1970-01-01
    • 1970-01-01
    • 2016-05-08
    • 2014-05-19
    • 2017-11-23
    • 2014-07-05
    相关资源
    最近更新 更多