Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It is a lot of typing (~50 lines) but when you finish the basic functionality you can easily do things like change the item display or add expand/collapse functionality for the groups, here is an example (with expand/collapse, just for fun):</p> <p>First our data objects:</p> <pre><code>public class Item { public string Type { get; set; } public string Name { get; set; } public ImageSource Icon { get; set; } } </code></pre> <p>Create them in your code and set a list of them as the DataContext of the following window:</p> <pre><code>&lt;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"&gt; &lt;Window.Resources&gt; &lt;CollectionViewSource x:Key="Items" Source="{Binding}"&gt; &lt;CollectionViewSource.GroupDescriptions&gt; &lt;PropertyGroupDescription PropertyName="Type"/&gt; &lt;/CollectionViewSource.GroupDescriptions&gt; &lt;/CollectionViewSource&gt; &lt;DataTemplate x:Key="ItemTemplate"&gt; &lt;Grid Width="128"&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="40"/&gt; &lt;RowDefinition Height="12"/&gt; &lt;/Grid.RowDefinitions&gt; &lt;Image Source="{Binding Icon}"/&gt; &lt;TextBlock Text="{Binding Name}" Grid.Row="1"/&gt; &lt;/Grid&gt; &lt;/DataTemplate&gt; &lt;ItemsPanelTemplate x:Key="ItemPanel"&gt; &lt;WrapPanel Orientation="Horizontal" Width="{Binding ElementName=W, Path=ActualWidth}"/&gt; &lt;/ItemsPanelTemplate&gt; &lt;DataTemplate x:Key="HeaderTemplate"&gt; &lt;StackPanel Margin="0 15"&gt; &lt;TextBlock Text="{Binding Name}"/&gt; &lt;Rectangle Height="1" Fill="Blue"/&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;Style x:Key="ContainerStyle" TargetType="{x:Type GroupItem}"&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate&gt; &lt;Expander Header="{Binding Name}" IsExpanded="True"&gt; &lt;ItemsPresenter/&gt; &lt;/Expander&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;/Window.Resources&gt; &lt;Grid&gt; &lt;ListBox ItemsSource="{Binding Source={StaticResource Items}}" ItemTemplate="{StaticResource ItemTemplate}" ItemsPanel="{StaticResource ItemPanel}"&gt; &lt;ListBox.GroupStyle&gt; &lt;GroupStyle HeaderTemplate="{StaticResource HeaderTemplate}" ContainerStyle="{StaticResource ContainerStyle}"/&gt; &lt;/ListBox.GroupStyle&gt; &lt;/ListBox&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>There may be some bugs in there but its a good start.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload