问题描述
向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>
结果:
这不是理想的布局,因为设置"被紧紧压在就绪"上.
This is not the ideal layout, since the "Set" is squeezed right up against the "Ready".
如何完全控制WPF StatusBar
控件的布局?
How do I gain full control over the layout of the WPF StatusBar
control?
推荐答案
默认情况下,StatusBar
使用DockPanel
定位其子级.这对于一件商品来说效果很好,但在与一个以上的孩子一起工作时,往往会使事情变得混乱和不便.
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.
要获得对状态栏子项位置的高度控制,可以将DockPanel
换成Grid
:
To gain a high level of control over the positioning of status bar children, you can swap out the 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>
结果是:
有关更深入的讨论,请访问我的博客文章此处.
For a more in-depth discussion, please visit my blog post here.
这篇关于如何自定义WPF StatusBar布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!