Bug 现象,点击Clear button后,操作一下UI,发现ListView的里仍然能保留第一行,太匪夷所思了。
以下是测试程序的代码:
<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>
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
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
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
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
}
}