如何自定义 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:57:09  来源:igfitidea点击:

How do I customize the WPF StatusBar layout?

wpflayoutcontrolsstatusbarstatusbaritem

提问by Kent Boogaart

Adding more than one child to a WPF StatusBarresults 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:

结果是:

enter image description here

在此处输入图片说明

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 StatusBarcontrol?

如何完全控制 WPFStatusBar控件的布局?

回答by Kent Boogaart

By default, the StatusBaruses a DockPanelto 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 DockPanelfor a Grid:

要获得对状态栏子项定位的高级控制,您可以将DockPanelfor 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:

这导致:

enter image description here

在此处输入图片说明

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 DockPanelnor 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 的时候。