WPF ListBox水平放置其项目

And*_*erd 54 data-binding wpf listbox

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

如果我想要窗口左侧的列表,垂直显示图像,我可以使用数据绑定非常优雅.

    <ListBox 
        Name="m_listBox"
        IsSynchronizedWithCurrentItem="True"
        ItemsSource="{Binding}"            
        >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Image Source="{Binding}" Width="60" Stretch="Uniform" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
Run Code Online (Sandbox Code Playgroud)

有一种直截了当的方法,我可以使这个水平而不是垂直?解决方案的主要要求是:

  • 使用数据绑定填充项目
  • 只需用户点击它即可更改所选项目.

ada*_*ost 116

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>
Run Code Online (Sandbox Code Playgroud)

WPF教程

  • 或StackPanel的Orientation ="Horizo​​ntal" (39认同)
  • Nir说,StackPanel将是更好的解决方案. (2认同)
  • 代表安德鲁梅格斯给你+1.谁发现你的代码如此有用,但又不想给你"假想的互联网点" (2认同)

Bob*_*ers 19

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

例:

<ListBox>
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel IsItemsHost="True" Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>
Run Code Online (Sandbox Code Playgroud)