Это много печатать (~ 50 строк), но когда вы закончите базовую функциональность, вы можете легко сделать такие вещи, как изменить отображение элемента или добавить функциональность развернуть / свернуть для групп, вот пример (с развернуть / свернуть , просто для удовольствия):
Первые наши объекты данных:
public class Item
{
public string Type { get; set; }
public string Name { get; set; }
public ImageSource Icon { get; set; }
}
Создайте их в своем коде и установите их список в качестве DataContext следующего окна:
<Window x:Class="ListViewTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300" Name="W">
<Window.Resources>
<CollectionViewSource x:Key="Items" Source="{Binding}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Type"/>
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
<DataTemplate x:Key="ItemTemplate">
<Grid Width="128">
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
<RowDefinition Height="12"/>
</Grid.RowDefinitions>
<Image Source="{Binding Icon}"/>
<TextBlock Text="{Binding Name}" Grid.Row="1"/>
</Grid>
</DataTemplate>
<ItemsPanelTemplate x:Key="ItemPanel">
<WrapPanel Orientation="Horizontal" Width="{Binding ElementName=W, Path=ActualWidth}"/>
</ItemsPanelTemplate>
<DataTemplate x:Key="HeaderTemplate">
<StackPanel Margin="0 15">
<TextBlock Text="{Binding Name}"/>
<Rectangle Height="1" Fill="Blue"/>
</StackPanel>
</DataTemplate>
<Style x:Key="ContainerStyle" TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Expander Header="{Binding Name}" IsExpanded="True">
<ItemsPresenter/>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<ListBox
ItemsSource="{Binding Source={StaticResource Items}}"
ItemTemplate="{StaticResource ItemTemplate}"
ItemsPanel="{StaticResource ItemPanel}">
<ListBox.GroupStyle>
<GroupStyle
HeaderTemplate="{StaticResource HeaderTemplate}"
ContainerStyle="{StaticResource ContainerStyle}"/>
</ListBox.GroupStyle>
</ListBox>
</Grid>
</Window>
Там могут быть некоторые ошибки, но это хорошее начало.