按值类型的ObservableCollection [英] ObservableCollection by Value Type
本文介绍了按值类型的ObservableCollection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我认为Code解释了我的问题
I think Code explains my problem
public ObservableCollection<Project> Project { get; set; }
private Project _current;
public Project Current
{
get { return _current; }
set
{
if (_current== value)
{
return;
}
_current= value;
NotifyOfPropertyChange(() => Current);
}
}
点击按钮点击
On a button Click
public void SelectPump()
{
Project.Add(Current);
for (var i = 0; i < Project.Count; i++)
{
Project[i].IdNo = i;
}
}
如果我的Project.Count()= 3,我所有的项目[i] .IdNo都会变为3希望它是
All my Project[i].IdNo is becoming 3 if my Project.Count() =3 I want it to be
Project[0].IdNo = 0;
Project[1].IdNo = 1;
Project[2].IdNo = 2;
推荐答案
代码示例:尝试根据此情况重新调整代码,看看会发生什么。
Code example: Just try to refacoter your code based on this and see what happens.
class FooObservableCollection : ObservableCollection<Foo>
{
protected override void InsertItem(int index, Foo item)
{
base.Add(index, Foo);
if (this.CollectionChanged != null)
this.CollectionChanged(this, new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Add, item, index);
}
}
var collection = new FooObservableCollection();
collection.CollectionChanged += CollectionChanged;
collection.Add(new Foo());
void CollectionChanged (object sender, NotifyCollectionChangedEventArgs e)
{
Foo newItem = e.NewItems.OfType<Foo>().First();
}
这篇关于按值类型的ObservableCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文