所以我有这个系列:
Dictionary<string, int> dictionary;
ObservableCollection<MyItem> list = new ObservableCollection<MyItem>();
public class MyItem
{
public string IP { get; set; }
public int Packets { get; set; }
public decimal Percent { get; set; }
}
我在这里填充ListView
:
foreach (KeyValuePair<string, int> item in dictionary.OrderByDescending(value => value.Value))
{
double value = ((double)item.Value / someValue) * 100;
MyItem myItem = new MyItem { IP = item.Key, Packets = item.Value, Percent = value };
list.Add(myItem);
listView.ItemsSource = list;
}
所以我想要的是逐个添加我的项目,目前的状态是我只能在ListView
结束时看到所有项目。
答案 0 :(得分:2)
这是预期的行为:当您使用ObservableCollection设置ItemSource
时,或者在添加项目后设置ObservableCollection属性。
要做到这一点,您希望在添加项目之前必须设置项目来源。
理想的做法是将ItemSource绑定到ObservableCollection,然后在视图模型中填充可观察的集合。
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
namespace StackOverflow2
{
public partial class MainWindow : Window
{
readonly Dictionary<string, int> _dictionary;
public MainWindow()
{
InitializeComponent();
DataContext = this;
List = new ObservableCollection<MyItem>();
_dictionary = new Dictionary<string, int>();
//bogus initial dictionary fill
for (int i = 0; i < 2000; i++)
{
_dictionary.Add("Key_"+i,i+1);
}
}
private int someValue = 10;
public void Fillcollection()
{
Task.Factory.StartNew(() =>
{
foreach (KeyValuePair<string, int> item in _dictionary.OrderByDescending(value => value.Value))
{
decimal value = ((decimal)item.Value / someValue) * 100;
MyItem myItem = new MyItem { IP = item.Key, Packets = item.Value, Percent = value };
this.Dispatcher.Invoke(()=>List.Insert(0,myItem));
Thread.Sleep(50);
}
});
}
public ObservableCollection<MyItem> List { get; set; }
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
Fillcollection();
}
}
public class MyItem
{
public string IP { get; set; }
public int Packets { get; set; }
public decimal Percent { get; set; }
}
}
<Window x:Class="StackOverflow2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:StackOverflow2"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance {x:Type local:MainWindow}}"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button Content="FillCollection" Click="ButtonBase_OnClick" ></Button>
<DataGrid ItemsSource="{Binding List}" Grid.Column="1" AutoGenerateColumns="True"></DataGrid>
</Grid>
</Window>