我在其他viewModel(ScenarioManager)中获得了viewModels(InputViewModel)的集合。 每个InputviewModel都有一个Class(RestOfInput)实例,该实例包含能够引发OnPropertyChanged的属性。 当这些属性中的一个改变该事件由该方法(在InputViewModel)处理:OnPropertyChanged反应第一次和第二次不同
public void TestAfterChanges(object sender, PropertyChangedEventArgs e)
{
MessageBox.Show("not ref");
bool isInTheList = false;
RestOfInput roi = sender as RestOfInput;
string prop = e.PropertyName;
if (prop!="NameFile")
{
Difference d = new Difference();
d.Length = prop;
d.Value1 = reference.RoI.getValueByPropertyName(prop);
d.Value2 = roi.getValueByPropertyName(prop);
foreach (Difference diff in _ListOfDifferences)
{
if (diff.Length==prop)
{
if ((Math.Abs(d.Value2-d.Value1)>0.001*d.Value1))
{
//replace by le new one
_ListOfDifferences.Insert(_ListOfDifferences.IndexOf(diff), d);
_ListOfDifferences.Remove(diff);
}
else
{
//if change make the field value equal to the ref then remove from difference list
_ListOfDifferences.Remove(diff);
}
isInTheList = true;
}
}
if ((Math.Abs(d.Value2 - d.Value1) > 0.001 * d.Value1) && isInTheList==false)
{
_ListOfDifferences.Add(d);
}
}
}
此方法给出只是一个该特定情况和基准情况之间的差异汇总。
现在,如果引用情况的变化我要更新所有的案件和事件在ScenarioManager处理 :
public void refCaseChanging(object sender, PropertyChangedEventArgs e)
{
MessageBox.Show("ref");
string propname = e.PropertyName;
foreach (InputViewModel item in _casesList)
{
if (item!=inpVM)
{
item.RoI.OnPropertyChanged(propname);
}
}
}
inpVM是参考案例。
然后,我有这样的行为: - 如果我改变了一个案件,而不是参考案例的领域:一切都很好。 - 如果我改变参考案例中的特定领域:第一次,一切都很好。 但是第二次,只有参考案例和非参考案例中的第一个案例(在集合中)更新> 这就像foreach循环被打破。
任何解释。
如果消息是不明确的,请告诉我(不容易解释;))
我认为Timores正在为此付出代价。我可以在item.RoI处看到一个可能的空引用异常(不要假设if(item!= inpVM)意味着item不为空)并且异常在执行树中的某处被捕获,而您并不知道。 +1 – 2010-10-06 06:23:49
Timores感谢您的回答非常有用:问题出在第一种方法的foreach循环上。由于我修改了Collection _ListOfDifference,因此循环无法结束。 – Gerrrard 2010-10-06 06:27:51
Tri Q,没有测试只是因为参考案例属于集合,但这个不能被删除。虽然永远不会是null.Thanks你 – Gerrrard 2010-10-06 06:32:41