Search code examples
wpfxamllayoutitemscontrolstackpanel

StackPanel orientation not fully horizontal on ItemsControl


I have the following issue. i'm using Items control and I dont understand why my result is on several lines instead of being on a single line. This is my code :

<StackPanel Orientation="Horizontal" Margin="10,10,10,10">
    <ItemsControl ItemsSource="{Binding Countries}" >
        <ItemsControl.ItemTemplate>
            <DataTemplate x:Name="TabCountries" >
                <StackPanel Orientation="Horizontal">
                    <TextBlock Name="Country" Text="{Binding nom}" Style="{StaticResource whiteFontColor}" VerticalAlignment="Center" Margin="0,0,5,0"/>
                    <CheckBox Margin="0,0,5,0" Name="isCountryAllowed" IsChecked="{Binding isAllowed}" />
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</StackPanel>

The result is like that :

FR
BE
AN

instead of being like that:

FR BE AN ...

Any ideas ?


Solution

  • You did not change the layout of the items, only the layout of the ItemsControl as a whole (which does almost nothing if there are no other elements in the same StackPanel).

    Use ItemsControl.ItemsPanel to make the items layout horizontally.

    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>