如何自定义 WPF 状态栏布局?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1373529/
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
How do I customize the WPF StatusBar layout?
提问by Kent Boogaart
Adding more than one child to a WPF StatusBar
results in poor layout with little option to customize. For example, this code:
将多个子项添加到 WPF 会StatusBar
导致布局不佳,几乎没有自定义选项。例如,这段代码:
<Window x:Class="StatusBar.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>
<StatusBar DockPanel.Dock="Bottom">
<StatusBarItem>
<TextBlock>Ready</TextBlock>
</StatusBarItem>
<StatusBarItem>
<TextBlock>Set</TextBlock>
</StatusBarItem>
</StatusBar>
<Label>Main Content</Label>
</DockPanel>
</Window>
Results in:
结果是:
This is not the ideal layout, since the "Set" is squeezed right up against the "Ready".
这不是理想的布局,因为“Set”紧靠“Ready”。
How do I gain full control over the layout of the WPF StatusBar
control?
如何完全控制 WPFStatusBar
控件的布局?
回答by Kent Boogaart
By default, the StatusBar
uses a DockPanel
to position its children. This works fine for one item, but tends to make things messy and inconvenient when working with more than one child.
默认情况下,StatusBar
使用 aDockPanel
来定位其子项。这适用于一件物品,但在与多个孩子一起工作时往往会使事情变得混乱和不方便。
To gain a high level of control over the positioning of status bar children, you can swap out the DockPanel
for a Grid
:
要获得对状态栏子项定位的高级控制,您可以将DockPanel
for a换掉Grid
:
<Window x:Class="StatusBar.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>
<StatusBar DockPanel.Dock="Bottom">
<StatusBar.ItemsPanel>
<ItemsPanelTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="4*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
</Grid>
</ItemsPanelTemplate>
</StatusBar.ItemsPanel>
<StatusBarItem>
<TextBlock>Ready</TextBlock>
</StatusBarItem>
<StatusBarItem Grid.Column="1">
<ProgressBar Value="30" Width="80" Height="18"/>
</StatusBarItem>
<StatusBarItem Grid.Column="2">
<TextBlock>Set</TextBlock>
</StatusBarItem>
<StatusBarItem Grid.Column="3">
<TextBlock>Go!</TextBlock>
</StatusBarItem>
</StatusBar>
<Label>Main Content</Label>
</DockPanel>
</Window>
This results in:
这导致:
For a more in-depth discussion, please visit my blog post here.
为了更深入的讨论,请访问我的博客文章在这里。
回答by Afshin
Actually, following Kent's reply I tried this and it works fine:
实际上,按照肯特的回复,我尝试了这个并且效果很好:
<StatusBar>
<StatusBarItem DockPanel.Dock="Right">
<TextBlock>Go!</TextBlock>
</StatusBarItem>
<StatusBarItem DockPanel.Dock="Right">
<TextBlock>Set</TextBlock>
</StatusBarItem>
<StatusBarItem DockPanel.Dock="Right">
<ProgressBar Value="30" Width="80" Height="18"/>
</StatusBarItem>
<!-- Fill last child is true by default -->
<StatusBarItem>
<TextBlock>Ready</TextBlock>
</StatusBarItem>
</StatusBar>
回答by Robin Maben
Just for the sake of reference for those reading the excellent answers above I'd like to suggest something even more simpler that achieves the same results. (Using neither DockPanel
nor StatusBar
).
仅供阅读上述优秀答案的人参考,我想建议一些更简单的方法来实现相同的结果。(既不使用DockPanel
也不使用StatusBar
)。
<Window>
.
.
<Grid Margin="2">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="15"/>
</Grid.RowDefinitions>
<SomeContainer Grid.Row="0" /> <!-- Main Content.. -->
<Grid Grid.Row="1">
<!-- Status bar laid out here (using similar approach)-->
</Grid>
</Window>
Disclaimer : This was long ago at a time when I was starting out with WPF.
免责声明:这是很久以前我开始使用 WPF 的时候。