网格内的WPF ListView,滚动错误 - ItemsControl与其项目源不一致

时间:2013-02-28 13:51:02

标签: c# wpf listview

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

ItemsControl与其商品来源

不一致

这是我的listView

<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);
    }
}

如何阻止此错误以及为什么会发生错误?

1 个答案:

答案 0 :(得分:3)

您正在将ListView的ItemSource设置为空列表。

将行lv_emails.ItemsSource = emails;移到循环之后。

或者你可以绑定到我在评论中提到的正确的集合类型。