1

我有一个包含几百个项目的 ListView,如果我通过滚动条拇指上的鼠标快速向下滚动项目,我会收到此错误:

ItemsControl 与其项目源不一致

这是我的列表视图

<ListView x:Name="lv_emails"
        Grid.Row="0"
        Background="{x:Null}"
        BorderBrush="{x:Null}"
        BorderThickness="0"
        ScrollViewer.HorizontalScrollBarVisibility="Auto"
        ScrollViewer.VerticalScrollBarVisibility="Auto"
        SelectionChanged="lv_emails_SelectionChanged"
        SelectionMode="Single">

内容在 Window_Loaded 中加载,没有线程进行:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    emails = new List<eml>();
    lv_emails.ItemsSource = emails;

    // get the emails of the selected folder and show them in the ListView
    eml em;
    foreach (string fi in Directory.GetFiles(path))
    {
        string[] split = fi.Split('}');//[0]path, [1]uid, [2]email, [3]date, [4]subject
        string disp = split[2] + " " + split[3] + " " + split[4]; 
        em = new eml { filePath = fi, id = split[1], emailAddress = split[2], date = split[3], subject = split[4] };
        emails.Add(em);
    }
}

我怎样才能停止这个错误,为什么会发生?

4

1 回答 1

3

You are setting the ListView's ItemSource to an empty List.

Move the line lv_emails.ItemsSource = emails; to after your loop.

Alternatively you could bind to the proper collection type as I mentioned in my comment.

于 2013-02-28T23:05:27.047 回答