2015-10-06 30 views
0

我有ListView我想填充。 我想要做的就是讓我的List與所有項目,並將此添加到我的ListView,但我希望它是逐漸。將文件逐個添加到我的ListView中

List

Dictionary<string, double> collection; 

型號:

public class MainViewModel 
{ 
    public DataTable PieData { get; private set; } 

    public MainViewModel() 
    { 
     this.PieData = GetTestData(); 
    } 

    private static DataTable GetTestData() 
    { 
     DataTable dtData = new DataTable("DATA"); 

     dtData.Columns.Add(new DataColumn("Name", typeof(string))); 
     dtData.Columns.Add(new DataColumn("Value", typeof(double))); 
     foreach (KeyValuePair<string, double> item in collection) 
      dtData.Rows.Add(new object[] { item.Key, item.Value }); 

     return dtData; 
    } 
} 

我的計時器:

private DispatcherTimer timer; 

public void CreateTimer() 
{ 
    timer = new DispatcherTimer(); 
    timer.Tick += timer_Tick; 
    timer.Interval = new TimeSpan(0, 0, 0, 0, 100); 
} 

添加到我的ListView通過我的計時器:

private void timer_Tick(object sender, EventArgs e) 
{ 
    foreach (KeyValuePair<string, double> item in collection) 
     ipStatisticsListView.Items.Add(new MyItem { IP = item.Key, Percent = item.Value }); 
} 

目前是什麼情況是,雖然我每間申報100毫秒添加操作我有延遲半秒,比我能看到裏面我所有的名單我LisView

+2

據我所知,這將* all *項目每100毫秒添加到列表視圖。 –

回答

0

我字典必須是一個列表,如果一個鍵可以包含不止一個價值。所以使用下面的方法之一。字典不會創建值的副本,因爲字典與數據表中的行值之間存在鏈接。

  DataTable dtData = new DataTable("DATA"); 
      Dictionary<string, List<double>> collection1 = dtData.AsEnumerable() 
       .GroupBy(x => x.Field<string>("Name"), y => y.Field<double>("Value")) 
       .ToDictionary(x => x.Key, y => y.ToList()); 

      Dictionary<string, double> collection2 = dtData.AsEnumerable() 
       .GroupBy(x => x.Field<string>("Name"), y => y.Field<double>("Value")) 
       .ToDictionary(x => x.Key, y => y.FirstOrDefault()); 
​ 
+0

我需要做什麼與collection1或collection2? –

0

出現這種情況的原因是因爲當一個新項目被添加到ListView控制,有導致重繪自身控制一個無效事件。如果要添加的項目之間的頻率太低,添加新項目可能會導致控件再次使自己失效,因此「暫停」繪製列表。

也許當ListView達到內容邊界內可見項的最大數量時,每當添加新項目時不再需要重新繪製自己,因此它可以繪製自己。

您是否嘗試增加計時器滴答之間的間隔以查看是否出現同一問題?

+0

是的,我甚至嘗試500毫秒 –