带有工具栏和状态栏的 WPF DockPanel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19930953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF DockPanel with Toolbar and Statusbar
提问by Ivan-Mark Debono
I'm learning WPF and am trying to have a form that consists of a toolbar at the top, a statusbar at the bottom and the rest will be occupied by controls used for data entry.
我正在学习 WPF 并尝试创建一个表单,该表单由顶部的工具栏、底部的状态栏组成,其余部分将由用于数据输入的控件占据。
This is what I have so far:
这是我到目前为止:
<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>
How do I add a statusbar at the bottom and another panel that will occupy the rest of the form?
如何在底部添加一个状态栏和另一个将占据表单其余部分的面板?
回答by Ehsan Hafeez
You can use DockPanel to arrange your controls. like this Toolbar will be dock at top, Statusbar at bottom and rest space will be assigned to datagrid because we have set the "LastChildFill" to true in 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}" />


回答by chao wang
Suggest you use Grid which can handle complex layout.
建议您使用可以处理复杂布局的 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>
回答by Rob
This is what I got playing around with your code:
这是我玩你的代码:
<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>

