WPF DockPanel带有工具栏和状态栏

4

我正在学习WPF,尝试创建一个表单,顶部是工具栏,底部是状态栏,其余区域用于数据输入控件。

目前我的进展如下:

<Window x:Class="MyApp.MyForm"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MyForm" Height="346" Width="459">
    <DockPanel>
        <ToolBarTray DockPanel.Dock="Top">
            <ToolBar>
                <Button Command="New" Content="New" />
                <Button Command="Open" Content="Open" />
                <Button Command="Save" Content="Save" />
            </ToolBar>
        </ToolBarTray>
    </DockPanel>
</Window>

我该如何在底部添加状态栏以及另一个占据表单其余部分的面板?
3个回答

11

您可以使用DockPanel来排列控件。像这样 工具栏将停靠在顶部,状态栏将停靠在底部,其余空间将分配给数据网格,因为我们在DockPanel中设置了"LastChildFill"为true。

<Window x:Class="MyApp.MyForm"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MyForm" Height="346" Width="459">
<DockPanel LastChildFill="True">
    <ToolBarTray DockPanel.Dock="Top">
        <ToolBar>
            <Button Command="Edit" Content="Edit" />
            <Button Command="Delete" Content="Delete" />
            <Button Command="Refresh" Content="Refresh" />
        </ToolBar>
    </ToolBarTray>
<StatusBar Name="statusbar" DockPanel.Dock="Bottom">statusbar</StatusBar>
<DataGrid Name="grdEmployees" ItemsSource="{Binding EmpCollection}" />

这里输入图像描述


2
在看了你的例子之后,我终于让我的代码运行起来了。谢谢你。我没有意识到“主填充控件”实际上需要是最后添加的项目。我太习惯Winforms了。 - Krythic

0
建议您使用能够处理复杂布局的网格(Grid)。
 <Grid>
        <Grid.RowDefinitions>
            <RowDefinition x:Name="Toolbar1"  Height="50" />
            <RowDefinition x:Name="Toolbar2"  Height="50" />
            <RowDefinition x:Name="ForDataVisualize"  Height="*" />
            <RowDefinition x:Name="ForStatusbar" Height="25" />
        </Grid.RowDefinitions>
 </Grid>

-2

这是我用你的代码玩耍得到的结果:

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MyForm" Height="346" Width="459">
    <DockPanel Margin="0,0,0,-4">
        <ToolBarTray DockPanel.Dock="Top">
            <ToolBar>
                <Button Command="New" Content="New" />
                <Button Command="Open" Content="Open" />
                <Button Command="Save" Content="Save" />
            </ToolBar>
        </ToolBarTray>
        <StatusBar Height="21" VerticalAlignment="Bottom" DockPanel.Dock="Bottom" HorizontalAlignment="Right" Width="431" Margin="0,0,10,0"/>
        <Grid Height="267" VerticalAlignment="Top" Width="451" DockPanel.Dock="Left"/>
    </DockPanel>
</Window>

不需要固定高度。 - niyou

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接