Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>ICollectionView</code> cannot. <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.groupstyle.containerstyleselector.aspx" rel="nofollow noreferrer">GroupStyle.ContainerStyleSelector</a> can. Here is an example. </p> <p><strong>XAML:</strong></p> <pre><code>&lt;Window x:Class="WpfWindowDrag.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:WpfWindowDrag="clr-namespace:WpfWindowDrag" Title="Window1" Height="300" Width="300"&gt; &lt;Window.Resources&gt; &lt;!--Container style for a groupped item--&gt; &lt;Style x:Key="RegularContainerStyle" 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;!--Container style for root objects (i.e. non grouped)--&gt; &lt;Style x:Key="RootContainerStyle" TargetType="{x:Type GroupItem}"&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate&gt; &lt;ItemsPresenter /&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;!--This guy lets us select choose proper group for an item.--&gt; &lt;WpfWindowDrag:CountryStyleSelector x:Key="ContainerStyleSelector" RegularGroupStyle="{StaticResource RegularContainerStyle}" RootGroup="{StaticResource RootContainerStyle}" /&gt; &lt;/Window.Resources&gt; &lt;Grid&gt; &lt;ListView x:Name="lv"&gt; &lt;ListView.GroupStyle&gt; &lt;GroupStyle ContainerStyleSelector="{StaticResource ContainerStyleSelector}" /&gt; &lt;/ListView.GroupStyle&gt; &lt;ListView.View&gt; &lt;GridView&gt; &lt;GridViewColumn DisplayMemberBinding="{Binding Path=Name}" Header="Name" /&gt; &lt;/GridView&gt; &lt;/ListView.View&gt; &lt;/ListView&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p><strong>CS:</strong></p> <pre><code>using System; using System.Collections.ObjectModel; using System.ComponentModel; using System.Windows; using System.Windows.Controls; using System.Windows.Data; namespace WpfWindow { public partial class Window1 : Window { private const int NumberOfRecords = 20; private readonly ObservableCollection&lt;Person&gt; _myList; public Window1() { InitializeComponent(); var countries = new[] { "", "US", "China", "India", "Japan", "Ukraine" }; var countriesCount = countries.Length; _myList = new ObservableCollection&lt;Person&gt;(); var rnd = new Random(); for (int i = 0; i &lt; NumberOfRecords; i++) { int countryIndex = rnd.Next(countriesCount); _myList.Add(new Person() { Name = string.Format("Name {0}", i), Country = countries[countryIndex] }); } ICollectionView view = CollectionViewSource.GetDefaultView(_myList); view.GroupDescriptions.Add(new PropertyGroupDescription("Country")); view.SortDescriptions.Add(new SortDescription("Country", ListSortDirection.Ascending)); view.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending)); lv.ItemsSource = view; } } public class Person { public string Name { get; set; } public string Country { get; set; } } public class CountryStyleSelector : StyleSelector { public Style RegularGroupStyle { get; set; } public Style RootGroup { get; set; } public override Style SelectStyle(object item, DependencyObject container) { var cvg = item as CollectionViewGroup; if (cvg == null) { return base.SelectStyle(item, container); } return string.IsNullOrEmpty(cvg.Name as string) ? RootGroup : RegularGroupStyle; } } } </code></pre> <p>Keep in mind though, having items group automatically disables virtualization and impacts performance. If you need something really fast with thousands of items inside you'd probably write your own control or look for 3rd parties.</p> <p>Cheers, Anvaka.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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