简体   繁体   English

C#WPF XAML状态栏对齐

[英]C# WPF XAML status bar alignment

Everything is fine when the text is default on startup before , but when I add song name, status is moving to right and status bar are outside area. 当文本在启动之前默认为默认时,一切都很好,但是当我添加歌曲名称时,状态会移到右侧,状态栏不在区域内。 after

<StatusBar Margin="0,730,0,0" DockPanel.Dock="Bottom" >
        <WrapPanel >
        <Label x:Name="tytulUtworu" HorizontalAlignment="Left">music: none </Label>
        <Label x:Name="lblstatusPolaczenia" Margin="200,0,0,0" HorizontalAlignment="Center">status: disconnected</Label>
        <ProgressBar Width="100" Height="15" Value="50" Margin="200,0,0,0" HorizontalAlignment="Right" />
        </WrapPanel>
    </StatusBar>

How can I repair this? 我该如何修理?

Instead of using a WrapPanel , try using a Grid . 代替使用WrapPanel ,请尝试使用Grid That way you'll have fixed columns that won't affect the other elements if text is added 这样,您将获得固定的列,如果添加了文本,这些列将不会影响其他元素

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>

    <Label x:Name="tytulUtworu" HorizontalAlignment="Left">music: none </Label>
    <Label x:Name="lblstatusPolaczenia" Grid.Column="1" HorizontalAlignment="Center">status: disconnected</Label>
    <ProgressBar Width="100" Height="15" Value="50" Grid.Column="2" HorizontalAlignment="Right" />
</Grid>

You can adjust the Width on each ColumnDefinition to get better sized columns for your use 您可以调整每个ColumnDefinition上的Width ,以获得更佳尺寸的列供您使用

Firstly, ditch the margins. 首先,放弃利润。 They are not helping, they just make things less obvious as to how they should be aligned. 他们无济于事,只是使事情在如何对齐方面不太明显。

From your margin values, I'm guessing that you want the status bar to show the label on the left, another in the center, and the progress bar on the right. 根据您的边距值,我猜测您希望状态栏在左侧显示标签,在中间显示另一个标签,在右侧显示进度条。

Below shows how to do this with a second dock panel. 下面显示了如何使用第二个扩展坞面板执行此操作。 Dock panel is ideal here as it is consistent in the way it reports its size to child components. 此处的Dock面板非常理想,因为它向子组件报告尺寸的方式是一致的。

If you want to place this status panel on another DockPanel , there are a few things which must be done before it will render as you need it to. 如果要将此状态面板放置在另一个DockPanel ,则需要做一些事情,然后它才能根据需要呈现。

Firstly, you main dock panel must have content, otherwise the boundary panels will not render in the right place. 首先,您的主停靠面板必须具有内容,否则边界面板将无法在正确的位置渲染。

Secondly, if you have set LastChildFill=True , you must add your main content as the last item. 其次,如果您设置LastChildFill=True ,则必须将主要内容添加为最后一项。 If you set the main content then add your status bar, even though you have docked it to the bottom, it will still try to fill the panel. 如果设置了主要内容,则添加状态栏,即使已将其停靠在底部,它仍会尝试填充面板。

I've updated the code snippet after testing it (my first attempt was typed straight into SO, sorry it didn't work). 在测试完代码片段后,我已经对其进行了更新(我的第一次尝试是直接输入SO,抱歉,它没有用)。

        <StatusBar DockPanel.Dock="Bottom"
               Name="statusBar"
               Height="50"
               HorizontalAlignment="Stretch"
               HorizontalContentAlignment="Stretch">
        <DockPanel LastChildFill="True"
                   Width="{Binding ActualWidth, ElementName=statusBar, Mode=OneWay}">
            <ProgressBar DockPanel.Dock="Right"
                         Width="100"
                         Value="50" />
            <Label x:Name="tytulUtworu"
                   DockPanel.Dock="Left"
                   ContentStringFormat="music: {0}"
                   Content="none" />
            <Label x:Name="lblstatusPolaczenia" 
                   HorizontalContentAlignment="Center"
                   Content="disconnected"
                   ContentStringFormat="status: {0}" />
        </DockPanel>
    </StatusBar>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM