C# 在 StackPanel wpf 中包装内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10146428/
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
wrapping content in a StackPanel wpf
提问by user1202434
is it possible to wrap content in a StackPanel?
是否可以将内容包装在 a 中StackPanel?
I know that we can make use of a WrapPanelinstead.
But for code modifying reasons, I must make use of a StackPanel.
我知道我们可以使用 aWrapPanel代替。但出于代码修改的原因,我必须使用StackPanel.
So, is there a way to make the items in a StackPanelwrap after say 5 items...
Thanks!
那么,有没有办法StackPanel在说 5 个项目之后将项目包装起来......谢谢!
采纳答案by Douglas
Create nested StackPanels which contain the required number of items.
创建StackPanel包含所需数量项目的嵌套s。
In the example below, you have two rows, respectively occupied by the <StackPanel Orientation="Horizontal">elements, which in turn each contain five items that will be displayed horizontally next to each other.
在下面的示例中,您有两行,分别由<StackPanel Orientation="Horizontal">元素占据,每行又包含五个将彼此水平显示的项目。
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<Item1 />
<Item2 />
<Item3 />
<Item4 />
<Item5 />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Item1 />
<Item2 />
<Item3 />
<Item4 />
<Item5 />
</StackPanel>
</StackPanel>
回答by shriek
Depending on your scenario you could use a UniformGrid. A few example can also be found here.
根据您的情况,您可以使用UniformGrid。一些示例也可以在这里找到。
You can define it to wrap after 5 Items like this.
您可以将其定义为在 5 个这样的项目之后换行。
<UniformGrid Columns="5">
<Button />
<Button />
<Button />
</UniformGrid>
Each Item will, however get the exact same width, so not sure if this will work for you.
但是,每个项目都将获得完全相同的宽度,因此不确定这是否适合您。
回答by NoviceProgrammer
I don't think you can do it without a wrap panel. Maybe you can try putting a wrapPanel inside the stack panel - set its width to to the Actual width of the stack panel. You can bind it like Width="{Binding ActualWidth, ElementName=StackPanel1}"
我认为如果没有包装面板,您就无法做到这一点。也许您可以尝试将 wrapPanel 放在堆栈面板内 - 将其宽度设置为堆栈面板的实际宽度。你可以像这样绑定它Width="{Binding ActualWidth, ElementName=StackPanel1}"
But this will be just a hack - i think wrap panel is the best suited for your needs.
但这只是一个黑客 - 我认为包装面板最适合您的需求。
回答by denis morozov
<StackPanel>
<StackPanel.Style>
<Style TargetType="{x:Type StackPanel}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type StackPanel}">
<WrapPanel/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</StackPanel.Style>
</StackPanel>
回答by bytecode77
For me, a simple WrapPanelworks just fine:
对我来说,一个简单的WrapPanel工作就好了:
<WrapPanel Orientation="Horizontal" Width="500" />
Not inside a StackPanelor any other container. And setting Widthto a constant value can be superior im some cases, because binding it to ActualWidthcan prevent down-sizing (e.g. when parent control is down-sized, WrapPanel is not)
不在StackPanel容器内或任何其他容器内。在某些情况下,将Width设置为常量值可能会更好,因为将其绑定到ActualWidth可以防止缩小(例如,当父控件缩小时,WrapPanel 不是)
回答by Joe Dobson
I put a stackpanel over the button. It won't affect button background. Then in the VB code I used Chr(12), to indicate a line feed:
我在按钮上放了一个堆栈面板。它不会影响按钮背景。然后在我使用的 VB 代码中Chr(12),表示换行:
Button1.Content = "first line" + Chr(12) + "second line"
You can add more lines using Chr(12).
您可以使用添加更多行Chr(12)。

