Bug. After clicking the Clear button, you can operate the UI and find that the first row can still be retained in the ListView, which is incredible.
The following is the code of the test program:
<Window x: Class = "WpfApplication37.MainWindow"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "MainWindow">
<Grid>
<Grid. RowDefinitions>
<RowDefinition/>
<RowDefinition Height = "25"/>
</Grid. RowDefinitions>
<ListView Name = "WaitList">
<ListView. View>
<GridView>
<GridView. Columns>
<GridViewColumn Header = "FirstName" DisplayMemberBinding = "{Binding Path = FirstName}"/>
<GridViewColumn Header = "LastName" DisplayMemberBinding = "{Binding Path = LastName}"/>
<GridViewColumn Header = "Age" DisplayMemberBinding = "{Binding Path = Age}"/>
</GridView. Columns>
</GridView>
</ListView. View>
</ListView>
<StackPanel Grid. Row = "1" Orientation = "Horizontal" HorizontalAlignment = "Center">
<Button Content = "Load" Click = "Load_Click"/>
<Button Content = "Clear" Click = "Clear_Click"/>
</StackPanel>
</Grid>
</Window>
Certificate ------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Windows;
Namespace WpfApplication37
{
/// <Summary>
/// Interaction logic for MainWindow. xaml
/// </Summary>
Public partial class MainWindow: Window
{
List <Person> pl = new List <Person> ();
PersonComparer personComparer = new PersonComparer () {sortDirection = ListSortDirection. Ascending };
Public MainWindow ()
{
InitializeComponent ();
WaitList. ItemsSource = pl;
}
Private void Clear_Click (object sender, RoutedEventArgs e)
{
Pl. Clear ();
MessageBox. Show ("Cleared ");
}
Private void Load_Click (object sender, RoutedEventArgs e)
{
For (int I = 0; I <40000; I ++)
{
Pl. Add (new Person (I. ToString (), "tome" + I, new Random (I). Next ()));
}
MessageBox. Show ("Loaded ");
}
}
Public class PersonComparer: IComparer <Person>
{
# Region IComparer <Person> Members
Public ListSortDirection sortDirection = new ListSortDirection ();
Public int Compare (Person x, Person y)
{
Return (sortDirection = ListSortDirection. Ascending? 1:-1) * (x. Age. CompareTo (y. Age ));
}
# Endregion
}
}
Certificate ------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Using System;
Using System. ComponentModel;
Namespace WpfApplication37
{
Public class Person: INotifyPropertyChanged
{
# Region [Constructor]
/// <Summary>
/// Initializes a new instance of the <see cref = "Person"/> class.
/// </Summary>
/// <Param name = "firstName"> The first name. </param>
/// <Param name = "lastName"> The last name. </param>
/// <Param name = "age"> The age. </param>
Public Person (String firstName, String lastName, Int32 age)
{
_ FirstName = firstName;
_ LastName = lastName;
_ Age = age;
}
# Endregion
# Region [Fields]
Private String _ firstName;
Private String _ lastName;
Private Int32 _ age;
# Endregion
# Region [Properties]
/// <Summary>
/// Gets or sets the first name.
/// </Summary>
/// <Value> The first name. </value>
Public String FirstName
{
Get {return _ firstName ;}
Set
{
If (_ firstName = value) return;
_ FirstName = value;
OnPropertyChanged ("FirstName ");
}
}
/// <Summary>
/// Gets or sets the last name.
/// </Summary>
/// <Value> The last name. </value>
Public String LastName
{
Get {return _ lastName ;}
Set
{
If (_ lastName = value) return;
_ LastName = value;
OnPropertyChanged ("LastName ");
}
}
/// <Summary>
/// Gets or sets the age.
/// </Summary>
/// <Value> The age. </value>
Public Int32 Age
{
Get {return _ age ;}
Set
{
If (_ age = value) return;
_ Age = value;
OnPropertyChanged ("Age ");
}
}
# Endregion
# Region [Events]
Public event PropertyChangedEventHandler PropertyChanged;
# Endregion
# Region [Methods]
/// <Summary>
/// Called when [property changed].
/// </Summary>
/// <Param name = "propertyName"> Name of the property. </param>
Protected void OnPropertyChanged (String propertyName)
{
If (PropertyChanged! = Null) PropertyChanged (this, new PropertyChangedEventArgs (propertyName ));
}
# Endregion
}
}