wpf - 水平显示 ItemsControl 内的项目集合

标签 wpf xaml panel datatemplate itemscontrol

这是 XAML 标记:

        <ScrollViewer Grid.Column="1" Grid.Row="2" HorizontalScrollBarVisibility="Disabled" Width="990">
            <StackPanel Margin="50 0 0 40">                    
                <ItemsControl x:Name="streamList" ItemsSource="{Binding StreamInformations}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Margin="10" Orientation="Horizontal" >
                                <StackPanel Orientation="Horizontal">
                                    <Image Source="{Binding ImageUrl}" Height="60" />
                                    <StackPanel Margin="10 0 0 0" VerticalAlignment="Center">
                                        <TextBlock Foreground="Black" Text="{Binding ChannelName}" FontSize="12" />
                                        <TextBlock Foreground="#999" Text="{Binding PlayerName}" FontSize="10" />
                                        <TextBlock Foreground="#999" Text="{Binding ViewCount}" FontSize="10" />
                                    </StackPanel>                                        
                                </StackPanel>                                   
                            </StackPanel>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </StackPanel>
        </ScrollViewer>

这就是它的外观:

enter image description here

我希望这些项目水平显示并在到达 StackPanel 边缘时向下流动。

目前看来,我的 DataContext 集合中的每个项目都在创建它自己的 StackPanel,所以这不是我想要的。

有什么建议么?

最佳答案

如果您将 ItemsPanel 模板更改为 WrapPanel 或水平 StackPanel,则可以达到您想要的效果...

<ItemsControl>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <!--other stuff here--> 
    </ItemsControl.ItemTemplate>
</ItemsControl>

在此代码段中,ItemsPanel 设置为水平方向的 WrapPanel。 ItemTemplate 将包括您已经拥有的绑定(bind)和样式...

关于wpf - 水平显示 ItemsControl 内的项目集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8773343/

相关文章:

sql-server - Windows 应用程序 (WPF) 的独立数据库

.net - 无法将 .Net 面板设置为可见

ajax - Firefox HTTP 连接在阻塞阶段花费大量时间

java - Eclipse项目无法运行

c# - 如何获取 RichTextBox 的行数

c# - TextBlock 中接受转义字符的等效 HTML 代码

c# - 如何在 C# 中打开程序集绑定(bind)日志记录?

xaml - Windows 8 Metro中是否没有用于xaml的DatePicker?

wpf - 修改 TabControl 的标题

xaml - 实现多选项目列表