一行中的 WPF XAML WrapPanel ListBox 项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16896208/
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
WPF XAML WrapPanel ListBox items in a row
提问by zyjespox
I'm forced to ask for help, while I'm not able to figure it out by myself. I'm working on WPF-XAML desktop application, in which GUI is mostly generated dynamically.
我被迫寻求帮助,而我自己却无法弄清楚。我正在开发 WPF-XAML 桌面应用程序,其中 GUI 主要是动态生成的。
My query regards styling of WrapPanel with ListBox items.
我的查询是关于 WrapPanel 与 ListBox 项目的样式。
Please find a piece of code from my usercontrol (.xaml):
请从我的用户控件 (.xaml) 中找到一段代码:
<DockPanel x:Name="xResultPanel">
<ListView x:Name="bResultPanel" ItemsSource="{Binding ResultList, UpdateSourceTrigger=PropertyChanged}">
<ListView.ItemTemplate>
<DataTemplate>
<Expander Header="{Binding GroupName}" Style="{DynamicResource FeatureExpander2}">
<WrapPanel ItemWidth="140" Orientation="Horizontal">
<ListBox x:Name="ListOfTiles" ItemsSource="{Binding VideoSamples}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Width="120" Margin="10" HorizontalAlignment="Left">
<Image />
<TextBlock />
</StackPanel
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</WrapPanel>
</Expander>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</DockPanel>
Above code returns ListBox items presented not in a row, but each item in new line.I tried to set MinWidth, Width etc for WrapPanel and ListBox, but with no result.
上面的代码返回 ListBox 项目不是在一行中,而是在新行中的每个项目。我试图为 WrapPanel 和 ListBox 设置 MinWidth、Width 等,但没有结果。
Thanks in advance for all related tips how to force WrapPanel to fill it's content horizontally.
预先感谢所有相关提示如何强制 WrapPanel 水平填充其内容。
回答by Daniel Hilgarth
The problem is that your WrapPanelonly has a single child: The ListBox. This means that the layouting is done by the ItemsPaneltemplate of the ListBox.
问题是您WrapPanel只有一个孩子:ListBox. 这意味着,布点是由做ItemsPanel的模板ListBox。
Try this instead:
试试这个:
<Expander Header="{Binding GroupName}" Style="{DynamicResource FeatureExpander2}">
<ListBox x:Name="ListOfTiles" ItemsSource="{Binding VideoSamples}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Width="120" Margin="10" HorizontalAlignment="Left">
<Image />
<TextBlock />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</Expander>

