250

Do you know any controls inherited from the ItemsControl that have horizontal orientation of items?

RichieHindle
  • 272,464
  • 47
  • 358
  • 399
user101375
  • 6,961
  • 9
  • 42
  • 43

5 Answers5

525

Simply change the panel used to host the items:

<ItemsControl ...>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>
Kent Boogaart
  • 175,602
  • 35
  • 392
  • 393
37

While the promoted answer is great, here's an alternative if you want the items to stretch.

<ItemsControl.ItemsPanel>                              
    <ItemsPanelTemplate>
        <UniformGrid Rows="1" />
    </ItemsPanelTemplate>
</ItemsControl.ItemsPanel>   
NielW
  • 3,626
  • 1
  • 30
  • 38
  • If you're using UWP, you'll need UWP-UniformGrid from here: https://github.com/rickapps/UWP-UniformGrid-Control. I just implemented it plus NielW's solution above. Really easy and solves the problem. – Gail Foad Dec 07 '17 at 16:47
12

This is an example of how to do horizontal scrolling within an ItemsControl.

Firstly the main window viewmodel class used to get/set the list of items we wish to display.

MainWindowViewModel.cs

using System.Collections.Generic;

namespace ItemsControl
{
   public class Item
   {
      public Item(string title)
      {
         Title = title;
      }

      public string Title { get; set; }
   }

   public class MainWindowViewModel
   {
      public MainWindowViewModel()
      {
         Titles = new List<Item>()
         {
            new Item("Slide 1"),
            new Item("Slide 2"),
            new Item("Slide 3"),
            new Item("Slide 4"),
            new Item("Slide 5"),
            new Item("Slide 6"),
            new Item("Slide 7"),
            new Item("Slide 8"),
         };
      }

      public List<Item> Titles { get; set; }
   }
}

The main window xaml for the view:

MainWindow.xaml

    <Window x:Class="ItemsControl.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ItemsControl"      
        mc:Ignorable="d"
        Title="MainWindow" Height="400" Width="400">

    <Window.DataContext>
        <local:MainWindowViewModel />
    </Window.DataContext>

    <Grid Margin="5">
        <ScrollViewer
            VerticalScrollBarVisibility="Disabled"
            HorizontalScrollBarVisibility="Auto">

            <ItemsControl
                x:Name="SearchResultList"                
                ItemsSource="{Binding Titles}">

                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel Orientation="Vertical"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>

                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Border
                            Margin="5"
                            BorderThickness="1"
                            BorderBrush="Aqua">

                            <TextBlock
                                Text="{Binding Title}"
                                HorizontalAlignment="Center"                               
                                VerticalAlignment="Top"
                                FontSize="12"
                                TextWrapping="Wrap"
                                TextAlignment="Center"
                                FontWeight="DemiBold"  
                                Width="150"
                                Height="150" />
                        </Border>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>

            </ItemsControl>
        </ScrollViewer>

    </Grid>
</Window>

Depending on how high/wide your client area is, this will result in this kind of layout, overflow items scrolled horizontally:

enter image description here

More details can be found at this blog link, including an example on how to do the scrolling vertically:

http://www.technical-recipes.com/2017/how-to-orient-wrappanel-items-within-itemscontrol-lists-vertically-and-horizontally/

AndyUK
  • 3,933
  • 7
  • 42
  • 44
9

The top answer is good, but I couldn't get it to work with UserControls. If you need UserControls, this should help.

ItemsControl with Horizontal Usercontrols

My Version:

<Window.Resources>
    <DataTemplate x:Key="ItemTemplate2">
        <StackPanel>
            <uc:MyUserControl MinWidth="20" BorderBrush="Black" BorderThickness="0.1" />
        </StackPanel>
    </DataTemplate>

    <ItemsPanelTemplate x:Key="ItemsPanelTemplate1">
        <StackPanel Orientation="Horizontal" Margin="0,0,0,0"/>
    </ItemsPanelTemplate>
</Window.Resources>

<StackPanel>
    <ItemsControl x:Name="list_MyControls"
                  HorizontalAlignment="Left"
                  VerticalAlignment="Top"
                  Margin="0,8,0,0"
                  ItemTemplate="{StaticResource ItemTemplate2}"
                  ItemsPanel="{StaticResource ItemsPanelTemplate1}" />
</StackPanel>

To bind to data, you will need to add an ItemsSource to the ItemsControl in the XAML or code behind. Also note that uc: would be the xmlns:uc="NamespaceOfMyControl" declared at the top of the file.

g t
  • 7,287
  • 7
  • 50
  • 85
JsAndDotNet
  • 16,260
  • 18
  • 100
  • 123
  • I'm not used to using WPF, so maybe what i will say is very basic stuff. I found out that inside an UserControl you should use "UserControl.Resources" instead of "Window.Resources". Anyway, thanks for the great answer, solved my problem. – Paulo André Haacke Dec 07 '17 at 12:23
0

For a long list with hundreds or even thousands of items, you should better use VirtualizingStackPanel to avoid performance issues.

Evgenii
  • 49
  • 6
  • this is an 11 year old question, I would hope it's been resolved already ;) – AntLaC Jul 31 '20 at 17:42
  • 1
    Just added this comment to make the solution more complete :) Because you know, this WPF, it's so unfriendly framework, sometimes unpredictable, and with latest VS 2019 crashes so annoying XD – Evgenii Aug 01 '20 at 19:49