Skip to content

Commit eea8a63

Browse files
michael-hawkerArlodotexe
authored andcommitted
Clean-up ACV sample to use x:Bind, remove redundant code in md file with embedded sample.
1 parent 5ff4d4f commit eea8a63

File tree

4 files changed

+33
-68
lines changed

4 files changed

+33
-68
lines changed

components/Collections/samples/AdvancedCollectionView.md

Lines changed: 7 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
22
title: AdvancedCollectionView
33
author: nmetulev
4-
description: The AdvancedCollectionView is a collection view implementation that support filtering, sorting and incremental loading. It's meant to be used in a viewmodel.
5-
keywords: AdvancedCollectionView, data, sorting, filtering, Collections
4+
description: The AdvancedCollectionView is a collection view implementation that support filtering, sorting and incremental loading. It's meant to be used in a view or viewmodel.
5+
keywords: AdvancedCollectionView, CollectionViewSource, data, sorting, filtering, Collections
66
dev_langs:
77
- csharp
88
category: Helpers
@@ -12,70 +12,23 @@ issue-id: 0
1212
icon: Assets/AdvancedCollectionView.png
1313
---
1414

15-
> [!Sample AdvancedCollectionViewSample]
16-
1715
## Usage
1816

19-
In your viewmodel instead of having a public [IEnumerable](https://learn.microsoft.com/dotnet/core/api/system.collections.generic.ienumerable-1) of some sort to be bound to an eg. [Listview](https://learn.microsoft.com/uwp/api/Windows.UI.Xaml.Controls.ListView), create a public AdvancedCollectionView and pass your list in the constructor to it. If you've done that you can use the many useful features it provides:
17+
In your view or viewmodel instead of having a public [IEnumerable](https://learn.microsoft.com/dotnet/core/api/system.collections.generic.ienumerable-1) of some sort to be bound to an eg. [Listview](https://learn.microsoft.com/uwp/api/Windows.UI.Xaml.Controls.ListView), create a public AdvancedCollectionView and pass your list in the constructor to it. If you've done that you can use the many useful features it provides:
2018

2119
* sorting your list using the `SortDirection` helper: specify any number of property names to sort on with the direction desired
2220
* filtering your list using a [Predicate](https://learn.microsoft.com/dotnet/core/api/system.predicate-1): this will automatically filter your list only to the items that pass the check by the predicate provided
2321
* deferring notifications using the `NotificationDeferrer` helper: with a convenient _using_ pattern you can increase performance while doing large-scale modifications in your list by waiting with updates until you've completed your work
2422
* incremental loading: if your source collection supports the feature then AdvancedCollectionView will do as well (it simply forwards the calls)
2523
* live shaping: when constructing the `AdvancedCollectionView` you may specify that the collection use live shaping. This means that the collection will re-filter or re-sort if there are changes to the sort properties or filter properties that are specified using `ObserveFilterProperty`
2624

27-
## Example
28-
29-
```csharp
30-
using CommunityToolkit.WinUI.Collections;
31-
32-
// Grab a sample type
33-
public class Person
34-
{
35-
public string Name { get; set; }
36-
}
25+
The `AdvancedCollectionView` is a good replacement for WPF's `CollectionViewSource`.
3726

38-
// Set up the original list with a few sample items
39-
var oc = new ObservableCollection<Person>
40-
{
41-
new Person { Name = "Staff" },
42-
new Person { Name = "42" },
43-
new Person { Name = "Swan" },
44-
new Person { Name = "Orchid" },
45-
new Person { Name = "15" },
46-
new Person { Name = "Flame" },
47-
new Person { Name = "16" },
48-
new Person { Name = "Arrow" },
49-
new Person { Name = "Tempest" },
50-
new Person { Name = "23" },
51-
new Person { Name = "Pearl" },
52-
new Person { Name = "Hydra" },
53-
new Person { Name = "Lamp Post" },
54-
new Person { Name = "4" },
55-
new Person { Name = "Looking Glass" },
56-
new Person { Name = "8" },
57-
};
58-
59-
// Set up the AdvancedCollectionView with live shaping enabled to filter and sort the original list
60-
var acv = new AdvancedCollectionView(oc, true);
61-
62-
// Let's filter out the integers
63-
int nul;
64-
acv.Filter = x => !int.TryParse(((Person)x).Name, out nul);
65-
66-
// And sort ascending by the property "Name"
67-
acv.SortDescriptions.Add(new SortDescription("Name", SortDirection.Ascending));
68-
69-
// Let's add a Person to the observable collection
70-
var person = new Person { Name = "Aardvark" };
71-
oc.Add(person);
27+
## Example
7228

73-
// Our added person is now at the top of the list, but if we rename this person, we can trigger a re-sort
74-
person.Name = "Zaphod"; // Now a re-sort is triggered and person will be last in the list
29+
The following is a complete example of how to perform ...
7530

76-
// AdvancedCollectionView can be bound to anything that uses collections.
77-
YourListView.ItemsSource = acv;
78-
```
31+
> [!Sample AdvancedCollectionViewSample]
7932
8033
## Remarks
8134

components/Collections/samples/AdvancedCollectionViewSample.xaml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
1+
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
22
<Page x:Class="CollectionsExperiment.Samples.AdvancedCollectionViewSample"
33
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
44
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
55
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6-
xmlns:local="using:AdvancedCollectionViewExperiment.Samples"
6+
xmlns:local="using:CollectionsExperiment.Samples"
77
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
88
mc:Ignorable="d">
99

@@ -17,8 +17,9 @@
1717
<Setter Property="CornerRadius" Value="{StaticResource ControlCornerRadius}" />
1818
</Style.Setters>
1919
</Style>
20-
<DataTemplate x:Key="PersonDataTemplate">
21-
<TextBlock Text="{Binding Name}" />
20+
<DataTemplate x:Key="PersonDataTemplate"
21+
x:DataType="local:Person">
22+
<TextBlock Text="{x:Bind Name}" />
2223
</DataTemplate>
2324
</Page.Resources>
2425
<Grid ColumnSpacing="12"
@@ -51,7 +52,8 @@
5152
<Grid Grid.Row="2"
5253
Style="{StaticResource CardStyle}">
5354
<ListView x:Name="LeftList"
54-
ItemTemplate="{StaticResource PersonDataTemplate}" />
55+
ItemTemplate="{StaticResource PersonDataTemplate}"
56+
ItemsSource="{x:Bind Original}" />
5557
</Grid>
5658
<TextBlock Grid.Row="1"
5759
Grid.Column="1"
@@ -60,7 +62,8 @@
6062
Grid.Column="1"
6163
Style="{StaticResource CardStyle}">
6264
<ListView x:Name="RightList"
63-
ItemTemplate="{StaticResource PersonDataTemplate}" />
65+
ItemTemplate="{StaticResource PersonDataTemplate}"
66+
ItemsSource="{x:Bind CollectionView}" />
6467
</Grid>
6568
</Grid>
6669
</Page>

components/Collections/samples/AdvancedCollectionViewSample.xaml.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,29 @@
33
// See the LICENSE file in the project root for more information.
44

55
using CommunityToolkit.WinUI.Collections;
6+
using System.Diagnostics.CodeAnalysis;
67

78
namespace CollectionsExperiment.Samples;
89

9-
[ToolkitSample(id: nameof(AdvancedCollectionViewSample), "AdvancedCollectionView", description: $"A sample for showing how to create and use a {nameof(AdvancedCollectionView)}.")]
10+
[ToolkitSample(id: nameof(AdvancedCollectionViewSample), "AdvancedCollectionView", description: $"A sample for showing how to create and use a {nameof(AdvancedCollectionView)} for sorting and filtering.")]
1011
public sealed partial class AdvancedCollectionViewSample : Page
1112
{
12-
public ObservableCollection<Person>? oc;
13+
public ObservableCollection<Person> Original { get; private set; }
14+
15+
public AdvancedCollectionView CollectionView { get; private set; }
1316

1417
public AdvancedCollectionViewSample()
1518
{
1619
this.InitializeComponent();
1720
Setup();
1821
}
1922

23+
[MemberNotNull(nameof(Original))]
24+
[MemberNotNull(nameof(CollectionView))]
2025
private void Setup()
2126
{
2227
// left list
23-
oc = new ObservableCollection<Person>
28+
Original = new ObservableCollection<Person>
2429
{
2530
new Person { Name = "Staff" },
2631
new Person { Name = "42" },
@@ -40,22 +45,20 @@ private void Setup()
4045
new Person { Name = "8" },
4146
};
4247

43-
LeftList.ItemsSource = oc;
44-
4548
// right list
46-
var acv = new AdvancedCollectionView(oc);
49+
var acv = new AdvancedCollectionView(Original);
4750
int nul;
4851
acv.Filter = x => !int.TryParse(((Person)x).Name, out nul);
4952
acv.SortDescriptions.Add(new SortDescription("Name", SortDirection.Ascending));
5053

51-
RightList.ItemsSource = acv;
54+
CollectionView = acv;
5255
}
5356

5457
private void Add_Click(object sender, RoutedEventArgs e)
5558
{
5659
if (!string.IsNullOrWhiteSpace(NewItemBox.Text))
5760
{
58-
oc!.Insert(0, new Person { Name = NewItemBox.Text });
61+
Original.Insert(0, new Person { Name = NewItemBox.Text });
5962
NewItemBox.Text = "";
6063
}
6164
}

components/Collections/samples/Collections.Samples.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,10 @@
2323
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
2424
</Content>
2525
</ItemGroup>
26+
<ItemGroup>
27+
<PackageReference Include="PolySharp" Version="1.14.1">
28+
<PrivateAssets>all</PrivateAssets>
29+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
30+
</PackageReference>
31+
</ItemGroup>
2632
</Project>

0 commit comments

Comments
 (0)