@@ -37,10 +37,9 @@ For filtering to work, the ListView must have a data source that can be manipula
3737 Header =" Filter by Last Name"
3838 TextChanged =" OnFilterChanged" />
3939 <ListView x : Name =" FilteredListView"
40- ItemTemplate =" {StaticResource ContactListViewTemplate}" />
40+ ItemTemplate =" {StaticResource ContactListViewTemplate}" />
4141 </StackPanel >
4242</Grid >
43-
4443```
4544
4645## Filtering the data
@@ -67,85 +66,85 @@ using System.Linq;
6766
6867public sealed partial class MainPage : Page
6968{
70- // Define Contact collection to hold all Contact objects.
71- IList <Contact > allContacts = new List <Contact >();
72- // Define an ObservableCollection<Contact> object to serve as the ListView's
73- // ItemsSource. This collection will get updated after the filters are used:
74- ObservableCollection <Contact > contactsFiltered ;
69+ // Define Contact collection to hold all Contact objects.
70+ IList <Contact > allContacts = new List <Contact >();
71+ // Define an ObservableCollection<Contact> object to serve as the ListView's
72+ // ItemsSource. This collection will get updated after the filters are used:
73+ ObservableCollection <Contact > contactsFiltered ;
7574
76- public MainPage ()
77- {
78- this .InitializeComponent ();
79-
80- // Populate allContacts collection.
81- allContacts .Add (new Contact (" Kendall" , " Collins" , " Adatum Corporation" ));
82- allContacts .Add (new Contact (" Victoria" , " Burke" , " Bellows College" ));
83- allContacts .Add (new Contact (" Preston" , " Morales" , " Margie's Travel" ));
84- allContacts .Add (new Contact (" Miguel" , " Reyes" , " Tailspin Toys" ));
85-
86- // Populate contactsFiltered with all Contact objects (in this case,
87- // allContacts holds all of our Contact objects so we copy them into
88- // contactsFiltered). Set this newly populated collection as the
89- // ItemsSource for the ListView.
90- contactsFiltered = new ObservableCollection <Contact >(allContacts );
91- Filtereditemscontrol .itemssource = contactsFiltered ;
92- }
75+ public MainPage ()
76+ {
77+ this .InitializeComponent ();
78+
79+ // Populate allContacts collection.
80+ allContacts .Add (new Contact (" Kendall" , " Collins" , " Adatum Corporation" ));
81+ allContacts .Add (new Contact (" Victoria" , " Burke" , " Bellows College" ));
82+ allContacts .Add (new Contact (" Preston" , " Morales" , " Margie's Travel" ));
83+ allContacts .Add (new Contact (" Miguel" , " Reyes" , " Tailspin Toys" ));
84+
85+ // Populate contactsFiltered with all Contact objects (in this case,
86+ // allContacts holds all of our Contact objects so we copy them into
87+ // contactsFiltered). Set this newly populated collection as the
88+ // ItemsSource for the ListView.
89+ contactsFiltered = new ObservableCollection <Contact >(allContacts );
90+ Filtereditemscontrol .itemssource = contactsFiltered ;
91+ }
9392
94- // Whenever text changes in the filtering text box, this function is called:
95- private void OnFilterChanged (object sender , TextChangedEventArgs args )
96- {
97- // This is a Linq query that selects only items that return true after
98- // being passed through the Filter function, and adds all of those
99- // selected items to filtered.
100- var filtered = allContacts .Where (contact => Filter (contact ));
101- Remove_NonMatching (filtered );
102- AddBack_Contacts (filtered );
103- }
93+ // Whenever text changes in the filtering text box, this function is called:
94+ private void OnFilterChanged (object sender , TextChangedEventArgs args )
95+ {
96+ // This is a Linq query that selects only items that return true after
97+ // being passed through the Filter function, and adds all of those
98+ // selected items to filtered.
99+ var filtered = allContacts .Where (contact => Filter (contact ));
100+ Remove_NonMatching (filtered );
101+ AddBack_Contacts (filtered );
102+ }
104103
105- // The following functions are called inside OnFilterChanged:
104+ // The following functions are called inside OnFilterChanged:
106105
107- // When the text in any filter is changed, perform a check on each item in
108- // the original contact list to see if the item should be displayed. If the
109- // item passes the check, the function returns true and the item is added to
110- // the filtered list. Make sure all text is case-insensitive when comparing.
111- private bool Filter (Contact contact )
112- {
113- return contact .LastName .Contains
114- (FilterByLastName .Text , StringComparison .InvariantCultureIgnoreCase );
115- }
106+ // When the text in any filter is changed, perform a check on each item in
107+ // the original contact list to see if the item should be displayed. If the
108+ // item passes the check, the function returns true and the item is added to
109+ // the filtered list. Make sure all text is case-insensitive when comparing.
110+ private bool Filter (Contact contact )
111+ {
112+ return contact .LastName .Contains
113+ (FilterByLastName .Text , StringComparison .InvariantCultureIgnoreCase );
114+ }
116115
117- // These functions go through the current list being displayed
118- // (contactsFiltered), and remove any items not in the filtered collection
119- // (any items that don't belong), or add back any items from the original
120- // allContacts list that are now supposed to be displayed. (Adding/removing
121- // the items ensures the list view uses the desired add/remove animations.)
116+ // These functions go through the current list being displayed
117+ // (contactsFiltered), and remove any items not in the filtered collection
118+ // (any items that don't belong), or add back any items from the original
119+ // allContacts list that are now supposed to be displayed. (Adding/removing
120+ // the items ensures the list view uses the desired add/remove animations.)
122121
123- private void Remove_NonMatching (IEnumerable <Contact > filteredData )
124- {
125- for (int i = contactsFiltered .Count - 1 ; i >= 0 ; i -- )
122+ private void Remove_NonMatching (IEnumerable <Contact > filteredData )
126123 {
127- var item = contactsFiltered [i ];
128- // If contact is not in the filtered argument list,
129- // remove it from the ListView's source.
130- if (! filteredData .Contains (item ))
124+ for (int i = contactsFiltered .Count - 1 ; i >= 0 ; i -- )
131125 {
132- contactsFiltered .Remove (item );
126+ var item = contactsFiltered [i ];
127+ // If contact is not in the filtered argument list,
128+ // remove it from the ListView's source.
129+ if (! filteredData .Contains (item ))
130+ {
131+ contactsFiltered .Remove (item );
132+ }
133133 }
134134 }
135- }
136135
137- private void AddBack_Contacts (IEnumerable <Contact > filteredData )
138- {
139- foreach (var item in filteredData )
136+ private void AddBack_Contacts (IEnumerable <Contact > filteredData )
140137 {
141- // If the item in the filtered list is not currently in
142- // the ListView's source collection, add it back in.
143- if (! contactsFiltered .Contains (item ))
138+ foreach (var item in filteredData )
144139 {
145- contactsFiltered .Add (item );
140+ // If the item in the filtered list is not currently in
141+ // the ListView's source collection, add it back in.
142+ if (! contactsFiltered .Contains (item ))
143+ {
144+ contactsFiltered .Add (item );
145+ }
146146 }
147147 }
148- }
149148}
150149 ```
151150
0 commit comments