よくわかりませんがWPF系のプロジェクトじゃないと使えないらしい。WindowsFormのプロジェクトでは出てきませんでした。というこで、コードは感じ。
private void button1_Click(object sender, RoutedEventArgs e)
{
//using System.Collections.ObjectModel;
//using System.Collections.Specialized;
ObservableCollection<MyItem> collection = new ObservableCollection<MyItem>();
collection.CollectionChanged += new NotifyCollectionChangedEventHandler(collection_CollectionChanged);
collection.Add(new MyItem() { key = 1, value1 = "111" });
collection.Add(new MyItem() { key = 2, value1 = "222" });
collection.Add(new MyItem() { key = 3, value1 = "333" });
collection.Add(new MyItem() { key = 4, value1 = "444" });
collection[2] =new MyItem() { key = 5, value1 = "555" };
collection.RemoveAt(2);
//↓のように表示された
//action=Add NewItems=key=1
//action=Add NewItems=key=2
//action=Add NewItems=key=3
//action=Add NewItems=key=4
//action=Replace NewItems=key=5 OldItems=key=3
//action=Remove OldItems=key=5
}
void collection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("action={0} ",e.Action);
if (e.NewItems != null && e.NewItems.Count > 0)
{
sb.AppendFormat("NewItems={0} ", e.NewItems[0]);
}
if (e.OldItems !=null && e.OldItems.Count > 0)
{
sb.AppendFormat("OldItems={0} ", e.OldItems[0]);
}
System.Diagnostics.Debug.WriteLine(sb.ToString());
}
/// <summary>
/// コレクションに格納するクラス
/// </summary>
public class MyItem
{
public long key { get; set; }
public string value1 { get; set; }
public string value2 { get; set; }
public string value3 { get; set; }
public override string ToString()
{
return string.Format("key={0}", key);
}
}
{
//using System.Collections.ObjectModel;
//using System.Collections.Specialized;
ObservableCollection<MyItem> collection = new ObservableCollection<MyItem>();
collection.CollectionChanged += new NotifyCollectionChangedEventHandler(collection_CollectionChanged);
collection.Add(new MyItem() { key = 1, value1 = "111" });
collection.Add(new MyItem() { key = 2, value1 = "222" });
collection.Add(new MyItem() { key = 3, value1 = "333" });
collection.Add(new MyItem() { key = 4, value1 = "444" });
collection[2] =new MyItem() { key = 5, value1 = "555" };
collection.RemoveAt(2);
//↓のように表示された
//action=Add NewItems=key=1
//action=Add NewItems=key=2
//action=Add NewItems=key=3
//action=Add NewItems=key=4
//action=Replace NewItems=key=5 OldItems=key=3
//action=Remove OldItems=key=5
}
void collection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("action={0} ",e.Action);
if (e.NewItems != null && e.NewItems.Count > 0)
{
sb.AppendFormat("NewItems={0} ", e.NewItems[0]);
}
if (e.OldItems !=null && e.OldItems.Count > 0)
{
sb.AppendFormat("OldItems={0} ", e.OldItems[0]);
}
System.Diagnostics.Debug.WriteLine(sb.ToString());
}
/// <summary>
/// コレクションに格納するクラス
/// </summary>
public class MyItem
{
public long key { get; set; }
public string value1 { get; set; }
public string value2 { get; set; }
public string value3 { get; set; }
public override string ToString()
{
return string.Format("key={0}", key);
}
}
0 件のコメント:
コメントを投稿