C# 可观察收集<;T>;替换以及如何使DataModel触发属性更改
因此,除了我的代码的一部分之外,所有的工作都很好。我正在浏览一个可观察集合和一个列表,我正在将可观察集合中的一个元素设置为列表中的一个元素C# 可观察收集<;T>;替换以及如何使DataModel触发属性更改,c#,wpf,C#,Wpf,因此,除了我的代码的一部分之外,所有的工作都很好。我正在浏览一个可观察集合和一个列表,我正在将可观察集合中的一个元素设置为列表中的一个元素 someitemInOC = newListItem; 现在,CollectionChanged事件被触发,但问题是我还希望someitemInOC类型的所有属性也被触发。。。因为我有一个SomeType\u PropertyChanged函数,我想调用。。。。。。。。。。。。问题是我不知道怎样才能做到。显然,someitemInOC类型的所有属性都与se
someitemInOC = newListItem;
现在,CollectionChanged事件被触发,但问题是我还希望someitemInOC
类型的所有属性也被触发。。。因为我有一个SomeType\u PropertyChanged
函数,我想调用。。。。。。。。。。。。问题是我不知道怎样才能做到。显然,someitemInOC
类型的所有属性都与setter中的OnNotifyPropertyChanged
函数有关。。。但是如何触发所有属性以调用它们的notify?这是一个数据模型,不是viewmodel,但它确实继承了INotifyPropertyChanged
我的数据模型的一个片段
public sealed class WModel : INotifyPropertyChanged
{
private bool isD;
public bool IsD
{
get { return isD; }
set { isD = value; NotifyPropertyChanged(); }
}
private string wd;
public string Wd
{
get { return wd; }
set { wd = value; NotifyPropertyChanged(); }
}
public void UpdateAllParameters()
{
NotifyPropertyChanged(string.Empty);
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
在我的VM构造函数中,我有
WSource.CollectionChanged += WSource_CollectionChanged;
然后再往下走
private void WSource_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Remove)
{
foreach (WModel item in e.OldItems)
{
//Removed items
item.PropertyChanged -= WPropertyChanged;
}
}
else if (e.Action == NotifyCollectionChangedAction.Add)
{
foreach (WModel item in e.NewItems)
{
//Added items
item.PropertyChanged += WPropertyChanged;
}
}
}
public void WPropertyChanged(object sender, PropertyChangedEventArgs e)
{
Wd = WSource.Where(x => x.IsD).Count();
}
其中,Wd
是一个int
我希望在替换可观察集合中的项时调用WPropertyChanged函数
我试过做someitemInOC.UpdateAllParameters()
,但什么都没发生
someitemInOC = newListItem;
someitemInOC.UpdateAllParameters();
已调用NotifyPropertyChanged(string.Empty),但这对任何内容都没有影响。。。我看不到setter被击中或Notify函数被调用
成功了…
必须将此添加到CollectionChanged函数中
我的生活…最好有一个视图模型,以了解房地产的变化,看看fastDeepCloner,它会让你的生活变得更加轻松,这对我来说并不适用:-(我将发布更多代码来说明我的意思,进行了一些更改@ClemensYep…再次查看更新…开始工作了…)。。。。
else if (e.Action == NotifyCollectionChangedAction.Replace)
{
foreach (WModel item in e.OldItems)
{
//Removed items
item.PropertyChanged -= WPropertyChanged;
}
foreach (WModel item in e.NewItems)
{
//Added items
item.PropertyChanged += WPropertyChanged;
}
}