如何自定义 WPF StatusBar 布局?
向 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>
结果:
这不是理想的布局,因为“Set”是紧挨着“Ready”字样。
如何完全控制 WPF StatusBar
控件的布局?
Adding more than one child to a WPF StatusBar
results in poor layout with little option to customize. For example, this code:
<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".
How do I gain full control over the layout of the WPF StatusBar
control?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
默认情况下,
StatusBar
使用DockPanel
来定位其子项。这对于一件物品来说效果很好,但当与多个孩子一起工作时往往会让事情变得混乱和不方便。要获得对状态栏子项位置的高级控制,您可以将
DockPanel
替换为Grid
:这会导致:
要进行更深入的讨论,请访问我的博客文章 此处。
By default, the
StatusBar
uses aDockPanel
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.To gain a high level of control over the positioning of status bar children, you can swap out the
DockPanel
for aGrid
:This results in:
For a more in-depth discussion, please visit my blog post here.
实际上,根据肯特的回复,我尝试了这个,效果很好:
Actually, following Kent's reply I tried this and it works fine:
只是为了给那些阅读上面优秀答案的人提供参考,我想建议一些更简单的方法来达到相同的结果。 (既不使用
DockPanel
也不使用StatusBar
)。免责声明:这是很久以前的事了,当时我刚开始使用 WPF。
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
norStatusBar
).Disclaimer : This was long ago at a time when I was starting out with WPF.
您可以使用像 StackPanel 这样的容器将一组状态栏项目移动到右侧
You can use a container like StackPanel to move a group of status bar items to the right