<ListBox Background = "Black" ScrollViewer.VerticalScrollBarVisibility = "Auto">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation = "Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBoxItem Background = "White" Width = "150" Height = "200" VerticalAlignment = "Top" HorizontalAlignment = "Left" Margin = "5">Item0</ListBoxItem>
<ListBoxItem Background = "White" Width = "150" Height = "200" VerticalAlignment = "Top" HorizontalAlignment = "Left" Margin = "5">Item1</ListBoxItem>
<ListBoxItem Background = "White" Width = "150" Height = "200" VerticalAlignment = "Top" HorizontalAlignment = "Left" Margin = "5">Item2</ListBoxItem>
<ListBoxItem Background = "White" Width = "150" Height = "200" VerticalAlignment = "Top" HorizontalAlignment = "Left" Margin = "5">Item3</ListBoxItem>
<ListBoxItem Background = "White" Width = "150" Height = "200" VerticalAlignment = "Top" HorizontalAlignment = "Left" Margin = "5">Item4</ListBoxItem>
<ListBoxItem Background = "White" Width = "150" Height = "200" VerticalAlignment = "Top" HorizontalAlignment = "Left" Margin = "5">Item5</ListBoxItem>
<ListBoxItem Background = "White" Width = "150" Height = "200" VerticalAlignment = "Top" HorizontalAlignment = "Left" Margin = "5">Item6</ListBoxItem>
<ListBoxItem Background = "White" Width = "150" Height = "200" VerticalAlignment = "Top" HorizontalAlignment = "Left" Margin = "5">Item7</ListBoxItem>
</ListBox>
Этот код выше создает это изображение ниже.

Но я хочу создать что-то вроде этого изображения ниже. Где поле переходит на следующую строку в конце родительского элемента управления. И я хочу добиться этого с помощью XAML (и C#, если требуется)






Вам необходимо использовать WrapPanelи, чтобы отключить горизонтальную полосу прокрутки окна списка:
<ListBox Background = "Black" ScrollViewer.VerticalScrollBarVisibility = "Auto"
ScrollViewer.HorizontalScrollBarVisibility = "Disabled">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation = "Horizontal" ScrollViewer.HorizontalScrollBarVisibility = "Disabled" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBoxItem Background = "White" Width = "150" Height = "200" VerticalAlignment = "Top" HorizontalAlignment = "Left" Margin = "5">Item0</ListBoxItem>
<ListBoxItem Background = "White" Width = "150" Height = "200" VerticalAlignment = "Top" HorizontalAlignment = "Left" Margin = "5">Item1</ListBoxItem>
<ListBoxItem Background = "White" Width = "150" Height = "200" VerticalAlignment = "Top" HorizontalAlignment = "Left" Margin = "5">Item2</ListBoxItem>
<ListBoxItem Background = "White" Width = "150" Height = "200" VerticalAlignment = "Top" HorizontalAlignment = "Left" Margin = "5">Item3</ListBoxItem>
<ListBoxItem Background = "White" Width = "150" Height = "200" VerticalAlignment = "Top" HorizontalAlignment = "Left" Margin = "5">Item4</ListBoxItem>
<ListBoxItem Background = "White" Width = "150" Height = "200" VerticalAlignment = "Top" HorizontalAlignment = "Left" Margin = "5">Item5</ListBoxItem>
<ListBoxItem Background = "White" Width = "150" Height = "200" VerticalAlignment = "Top" HorizontalAlignment = "Left" Margin = "5">Item6</ListBoxItem>
<ListBoxItem Background = "White" Width = "150" Height = "200" VerticalAlignment = "Top" HorizontalAlignment = "Left" Margin = "5">Item7</ListBoxItem>
</ListBox>
Вот быстрый пример замены вашего StackPanel на WrapPanel. Он привязывает ширину WrapPanel к главному окну, так что элементы переносятся правильно даже при изменении размера окна.
<Window x:Class = "WpfApp1.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:WpfApp1"
mc:Ignorable = "d"
Title = "MainWindow" Height = "450" Width = "800" Name = "main">
<Grid>
<ListBox Background = "Black" ScrollViewer.VerticalScrollBarVisibility = "Auto">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation = "Horizontal" Width = "{Binding ElementName=main, Path=Width}"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBoxItem Background = "White" Width = "150" Height = "200" VerticalAlignment = "Top" HorizontalAlignment = "Left" Margin = "5">Item0</ListBoxItem>
<ListBoxItem Background = "White" Width = "150" Height = "200" VerticalAlignment = "Top" HorizontalAlignment = "Left" Margin = "5">Item1</ListBoxItem>
<ListBoxItem Background = "White" Width = "150" Height = "200" VerticalAlignment = "Top" HorizontalAlignment = "Left" Margin = "5">Item2</ListBoxItem>
<ListBoxItem Background = "White" Width = "150" Height = "200" VerticalAlignment = "Top" HorizontalAlignment = "Left" Margin = "5">Item3</ListBoxItem>
<ListBoxItem Background = "White" Width = "150" Height = "200" VerticalAlignment = "Top" HorizontalAlignment = "Left" Margin = "5">Item7</ListBoxItem>
<ListBoxItem Background = "White" Width = "150" Height = "200" VerticalAlignment = "Top" HorizontalAlignment = "Left" Margin = "5">Item7</ListBoxItem>
<ListBoxItem Background = "White" Width = "150" Height = "200" VerticalAlignment = "Top" HorizontalAlignment = "Left" Margin = "5">Item7</ListBoxItem>
<ListBoxItem Background = "White" Width = "150" Height = "200" VerticalAlignment = "Top" HorizontalAlignment = "Left" Margin = "5">Item7</ListBoxItem>
</ListBox>
</Grid>
</Window>
Возможно, придется немного поиграть с настройками, поскольку полосы прокрутки имеют тенденцию немного перекрывать плитки.
Просто измените свой ItemsPanelTemplate на
<ItemsPanelTemplate>
<WrapPanel MaxWidth = "800" Orientation = "Horizontal"/>
</ItemsPanelTemplate>
Вы можете сделать это, изменив свой ItemPanelTemplate, заменив StackPanel на WrapPanel. Вам также потребуется назначить MaxWidth WrapPanel ширине пользовательского элемента управления и ориентации по горизонтали. Например, если ваш пользовательский элемент управления называется «TestControl»
<ItemsPanelTemplate>
<WrapPanel MaxWidth = "{Binding ElementName=TestControl,Path=Width}" Orientation = "Horizontal"/>
</ItemsPanelTemplate>
Ваш ListBox будет выглядеть так.
<ListBox Background = "Black" ScrollViewer.VerticalScrollBarVisibility = "Auto">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel MaxWidth = "{Binding ElementName=TestControl,Path=Width}" Orientation = "Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBoxItem Background = "White" Width = "150" Height = "200" VerticalAlignment = "Top" HorizontalAlignment = "Left" Margin = "5">Item0</ListBoxItem>
<ListBoxItem Background = "White" Width = "150" Height = "200" VerticalAlignment = "Top" HorizontalAlignment = "Left" Margin = "5">Item1</ListBoxItem>
<ListBoxItem Background = "White" Width = "150" Height = "200" VerticalAlignment = "Top" HorizontalAlignment = "Left" Margin = "5">Item2</ListBoxItem>
<ListBoxItem Background = "White" Width = "150" Height = "200" VerticalAlignment = "Top" HorizontalAlignment = "Left" Margin = "5">Item3</ListBoxItem>
<ListBoxItem Background = "White" Width = "150" Height = "200" VerticalAlignment = "Top" HorizontalAlignment = "Left" Margin = "5">Item7</ListBoxItem>
<ListBoxItem Background = "White" Width = "150" Height = "200" VerticalAlignment = "Top" HorizontalAlignment = "Left" Margin = "5">Item7</ListBoxItem>
<ListBoxItem Background = "White" Width = "150" Height = "200" VerticalAlignment = "Top" HorizontalAlignment = "Left" Margin = "5">Item7</ListBoxItem>
<ListBoxItem Background = "White" Width = "150" Height = "200" VerticalAlignment = "Top" HorizontalAlignment = "Left" Margin = "5">Item7</ListBoxItem>
</ListBox>
Попробуйте WrapPanel вместо StackPanel