WPF:DockPanel.Dock = "Bottom"没有按预期工作

标签 wpf xaml dockpanel

这涉及到 WPF。

作为一个简单的测试,我有一个包含 3 列的网格,第三列包含一个 Dock 面板。反过来,这个 Dockpanel 包含一个 TextBlock 和一个 StatusBar,StatusBar 具有(附加的)属性 Dockpanel.Dock = "Bottom"。

因此,我希望 StatusBar 位于第 3 列的底部,并且 TextBlock - 上面写着“我是 TextBlock 3” - 在它的顶部(在同一列中)。

然而,令我惊讶的是,StatusBar 出现在 TextBlock 的右侧!

这是相关的xaml代码(我没有制作隐藏代码):

<Window x:Class="Testing.TestWindow"
        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:Testing"
        mc:Ignorable="d"
        Title="TestWindow" Height="500" Width="1000">

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" MinWidth="180" MaxWidth="540"  />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <TextBlock Grid.Column="0">I am Textblock 1</TextBlock>

        <TextBlock Grid.Column="1">I am Textblock 2</TextBlock>

        <DockPanel Grid.Column="2">
            <TextBlock>I am Textblock 3</TextBlock>
            <StatusBar DockPanel.Dock="Bottom">I am the statusbar</StatusBar>
        </DockPanel>
    </Grid>

</Window>

有谁知道我做错了什么?
谢谢。

最佳答案

更新您的 DockPanelLastChildFile="False"

  <DockPanel Grid.Column="2" LastChildFill="false">

该属性默认为 true,并且由于 StatusBarLastChildDockPanel ,您会看到您正在经历的行为。

LastChildFill 来自文档的信息:

true if the last child element stretches to fill the remaining space; otherwise false. The default value is true.

关于WPF:DockPanel.Dock = "Bottom"没有按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34364576/

相关文章:

wpf - MenuItem.Icon 仅出现在最后一项上

java - JPanels AS 坞站

wpf - 禁用 XamGrid 中的单元格编辑

c# - 如何在 DataGrid 中绑定(bind)两个依赖的 ComboBox

c# - 在 XAML 中使用 bool 值到颜色转换器

WPF Dockpanel 第一个子项使用剩余空间

wpf - 使 ComboBox 拉伸(stretch)到具有最大宽度和右对齐父项的可用空间

wpf - 如何在 WPF 中创建环绕按钮

wpf - 调整内容大小以适应屏幕分辨率

c# - ItemsControl.ItemsSource,绑定(bind)不起作用