如何在XAML中将多个StatusBarItem对齐到右侧? [英] How to align multiple StatusBarItems to the right side in XAML?
问题描述
我在C#应用程序上有一个 StatusBar
,其中包含4个项目.我基本上想将最后两个 StatusBarItems
浮动到右侧.我已经尝试通过将它们都设置为 HorizontalAlignment =" Right"
来进行尝试,但这仅对最后一项有效.
I have a StatusBar
with 4 items in it on my C# application. I basically want to float the last two StatusBarItems
to the right. I've tried it by setting them both with HorizontalAlignment="Right"
, but that did only work for the last item.
<StatusBar Name="statusBar1" Height="23" HorizontalAlignment="Stretch"
VerticalAlignment="Bottom">
<StatusBarItem />
<StatusBarItem />
<StatusBarItem HorizontalAlignment="Right" />
<StatusBarItem HorizontalAlignment="Right" />
</StatusBar>
我开始谷歌搜索,然后想到了以下网址:
I started googling and I came up with the following URL:
这真的是唯一的解决方案,还是有一种更简单的方法?
Is this really the only solution for this, or is there an easier way?
推荐答案
您可以利用以下事实: StatusBar
的默认 ItemsPanel
是DockPanel
.默认情况下, DockPanel
将尝试用最后一项填充剩余空间.因此,您添加到 StatusBar
的最后一个 StatusBarItem
将填充剩余的空间.要利用此优势,您可以像这样简单地嵌套 StatusBarItems
:
You can take advantage of the fact that the default ItemsPanel
for the StatusBar
is the DockPanel
. The DockPanel
will, by default, try to fill the remaining space with the last item. So the last StatusBarItem
you add to the StatusBar
will fill the remainder of the space. To take advantage of this, you can simply nest StatusBarItems
like this:
<StatusBar Name="statusBar1" Height="23" HorizontalAlignment="Stretch" VerticalAlignment="Bottom">
<StatusBarItem Content="Item 1"/>
<StatusBarItem Content="Item 2" />
<StatusBarItem HorizontalAlignment="Right">
<StackPanel Orientation="Horizontal">
<StatusBarItem Content="Item 3"/>
<StatusBarItem Content="Item 4"/>
<ProgressBar Height="15" Width="50" IsIndeterminate="True" Margin="5,0"/>
</StackPanel>
</StatusBarItem>
</StatusBar>
请注意,第三个 StatusBarItem
的 HorizontalAlignment
设置为 Right
,以便其内容将右对齐.
Note that the HorizontalAlignment
of the 3rd StatusBarItem
is set to Right
so that it's content will be right-aligned.
当然,您不必将Item 3和Item 4设置为 StatusBarItems
,它们可以是其他控件,例如 Buttons
或 ProgressBar
就像我上面演示的一样. StatusBarItem
只是一个将项目包装在 StatusBar
中的容器,类似于 ComboBoxItem
在 ComboBox 中包装项目的方式代码>.
Of course, you don't have to have Item 3 and Item 4 be StatusBarItems
, they could be other controls, such as Buttons
or ProgressBar
as I've demonstrated above as well. The StatusBarItem
is simply a container that wraps items in a StatusBar
, similar to how a ComboBoxItem
wraps items inside of a ComboBox
.
StatusBar
会自动将其内容包装在 StatusBarItems
中,如果您不使用它们,则第1项和第2项也很容易成为 TextBoxes
.使用 StatusBarItems
的主要原因是要控制 StatusBarItem
的工作方式,例如在第三个 StatusBarItem
中,它设置了手动 HorizontalAlignment
,而不是依赖默认值.
The StatusBar
will wrap it's contents in StatusBarItems
automatically, if you don't use them, so items 1 and 2 could just as easily be TextBoxes
. The primary reason to use StatusBarItems
is in the case where you want to control how the StatusBarItem
works, like in the 3rd StatusBarItem
where it sets the HorizontalAlignment
manually, rather than rely on the default.
这篇关于如何在XAML中将多个StatusBarItem对齐到右侧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!