水平排列项目的 WPF ListBox

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1292516/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:51:56  来源:igfitidea点击:

WPF ListBox that lays out its items horizontally

wpfdata-bindinglistbox

提问by Andrew Shepherd

I'm trying to write a WPF application for displaying images from a selection. I want to display all of the available images in a banner along the top of the window, and display the main selected image in the main window for further processing.

我正在尝试编写一个 WPF 应用程序来显示选择中的图像。我想在窗口顶部的横幅中显示所有可用图像,并在主窗口中显示主要选定图像以供进一步处理。

If I wanted the list on the Leftof the window, displaying the images vertically, I can do this quite elegantly using databinding.

如果我想要窗口左侧的列表,垂直显示图像,我可以使用数据绑定非常优雅地做到这一点。

    <ListBox 
        Name="m_listBox"
        IsSynchronizedWithCurrentItem="True"
        ItemsSource="{Binding}"            
        >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Image Source="{Binding}" Width="60" Stretch="Uniform" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

Is there a straightforward way I can make this horizontal instead of vertical? The main requirements of a solution is:

有没有一种直接的方法可以使这个水平而不是垂直?解决方案的主要要求是:

  • The items are populated using databinding
  • The selected item is changed simply by the user clicking it.
  • 使用数据绑定填充项目
  • 用户只需单击即可更改所选项目。

回答by adatapost

WrapPanel

包裹面板

 <ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel IsItemsHost="True" />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBoxItem>listbox item 1</ListBoxItem>
    <ListBoxItem>listbox item 2</ListBoxItem>
    <ListBoxItem>listbox item 3</ListBoxItem>
    <ListBoxItem>listbox item 4</ListBoxItem>
    <ListBoxItem>listbox item 5</ListBoxItem>
</ListBox>

WPF Tutorial

WPF 教程

回答by Bob Sammers

The default ItemsPanelfor the ListBoxcontrol is a VirtualizingStackPanel, so if you want the normal, default experience for the control but just have it laid out horizontally, you should specify this (and change the orientation).

默认ItemsPanelListBox控制是一个VirtualizingStackPanel,所以如果你想为控制正常,默认的经验,但只是把它水平放置,应指定这种情况(改变方向)。

Example:

例子:

<ListBox>
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel IsItemsHost="True" Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>