我的C#应用程序中有一个StatusBar,其中有4个项目。 我基本上想把最后两个StatusBarItems浮动到右边。 我已经通过将它们设置为HorizontalAlignment="Right"尝试它,但是这只对最后一项起作用。
<StatusBar Name="statusBar1" Height="23" HorizontalAlignment="Stretch" VerticalAlignment="Bottom"> <StatusBarItem /> <StatusBarItem /> <StatusBarItem HorizontalAlignment="Right" /> <StatusBarItem HorizontalAlignment="Right" /> </StatusBar>我开始使用Google,并且提出了以下内容: http : //kentb.blogspot.ch/2007/10/perfect-wpf-statusbar.html
这真的是唯一的解决方案吗? 还是有更简单的方法?
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: http://kentb.blogspot.ch/2007/10/perfect-wpf-statusbar.html
Is this really the only solution for this? Or is there an easier way?
最满意答案
您可以利用StatusBar的默认ItemsPanel是DockPanel的事实。 默认情况下, DockPanel会尝试用最后一项填充剩余空间。 因此,添加到StatusBar的最后一个StatusBarItem将填充剩余的空间。 为了利用这一点,你可以像这样简单地嵌套StatusBarItems :
<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以便其内容将右对齐。
当然,您不必将Item 3和Item 4设置为StatusBarItems ,它们可以是其他控件,例如Buttons或ProgressBar正如我上面演示的那样。 StatusBarItem只是一个容器,它包装StatusBar项目,类似于ComboBoxItem如何包装ComboBoxItem项目。
如果不使用它们, StatusBar将自动将其内容包装在StatusBarItems ,因此项目1和项目2可以很容易地成为TextBoxes 。 使用StatusBarItems主要原因是您想要控制StatusBarItem工作方式,就像手动设置HorizontalAlignment的第三个StatusBarItem一样,而不是依赖于默认值。
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>Note that the HorizontalAlignment of the 3rd StatusBarItem is set to Right so that it's content will be right-aligned.
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.
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.
更多推荐
HorizontalAlignment,Right,StatusBarItems,最后,电脑培训,计算机培训,IT培训"/> <m
发布评论