C# 实现可观测集合的问题

C# 实现可观测集合的问题,c#,wpf,binding,mvvm,observablecollection,C#,Wpf,Binding,Mvvm,Observablecollection,我有一个存储在数据库中的对象,我在viewmodel中检索该对象并将其放入可观察的集合中。这些对象是属性(房屋/不动产),每个属性都有一个称为图像的子对象。每个属性可以有多个图像(但每个图像只能有一个属性)。我只想使用一个viewmodel。我拥有填充列表框的属性,并且我可以成功地将图像绑定到后续的列表框,但前提是我必须通过iList进行绑定。我的问题是,如何将图像实现为它们自己的可观察集合(以便我可以监视更改),而不是iList。下面是我上面提到的一些特性的代码 public

我有一个存储在数据库中的对象,我在viewmodel中检索该对象并将其放入可观察的集合中。这些对象是属性(房屋/不动产),每个属性都有一个称为图像的子对象。每个属性可以有多个图像(但每个图像只能有一个属性)。我只想使用一个viewmodel。我拥有填充列表框的属性,并且我可以成功地将图像绑定到后续的列表框,但前提是我必须通过iList进行绑定。我的问题是,如何将图像实现为它们自己的可观察集合(以便我可以监视更改),而不是iList。下面是我上面提到的一些特性的代码

        public IList<Image> Images
    {
        get
        {
            if (CurrentProperty != null)
                return CurrentProperty.Images.ToList();
            return null;
        }
    }

 private void Load()
    {
        PropertyList = new ObservableCollection<Property>(from property in entities.Properties.Include("Images") select property);        

        //Sort the list (based on previous session stored in database)
        var sortList = PropertyList.OrderBy(x => x.Sort).ToList();
        PropertyList.Clear();
        sortList.ForEach(PropertyList.Add);

        propertyView = CollectionViewSource.GetDefaultView(PropertyList);         
        if (propertyView != null) propertyView.CurrentChanged += new System.EventHandler(propertyView_CurrentChanged);           


        public const string PropertiesPropertyName = "PropertyList";
    private ObservableCollection<Property> _PropertyList = null;

    public ObservableCollection<Property> PropertyList
    {
        get
        {
            return _PropertyList;
        }

        set
        {
            if (_PropertyList == value)
            {
                return;
            }

            var oldValue = _PropertyList;
            _PropertyList = value;

            // Update bindings, no broadcast
            RaisePropertyChanged(PropertiesPropertyName);
        }
    }    
公共IList图像
{
得到
{
如果(CurrentProperty!=null)
返回CurrentProperty.Images.ToList();
返回null;
}
}
专用空心荷载()
{
PropertyList=新的ObservableCollection(从entities.Properties.Include(“图像”)中的属性选择属性);
//对列表排序(基于数据库中存储的上一个会话)
var sortList=PropertyList.OrderBy(x=>x.Sort.ToList();
PropertyList.Clear();
sortList.ForEach(PropertyList.Add);
propertyView=CollectionViewSource.GetDefaultView(PropertyList);
if(propertyView!=null)propertyView.CurrentChanged+=new System.EventHandler(propertyView\u CurrentChanged);
public const string PropertiesPropertyName=“PropertyList”;
私有ObservableCollection_PropertyList=null;
公共可观测集合属性列表
{
得到
{
返回属性列表;
}
设置
{
如果(_PropertyList==值)
{
返回;
}
var oldValue=_PropertyList;
_PropertyList=值;
//更新绑定,无广播
RaisePropertyChanged(PropertiesPropertyName);
}
}    

最后通过以下问题的答案解决了问题:


我为图像创建了一个observablecollection,然后创建了一个新方法images_Update(),每当视图(属性observablecollection)的当前项发生更改时,该方法都会被调用。我还将其插入AddImages()和DeleteImages()的底部方法以确保在调用它们时更新列表。

如何更新图像?为什么只更新“一个视图模型”?我更新包含属性的observablecollection:CurrentProperty.images.Add(NewImage),然后将集合保存到数据库中。该程序实际上并不需要mvvm,使用它的唯一原因是利用observablecollections对列表框进行排序和重新排序。我相信这可以在一个视图模型中完成,如果我没有真正充分利用mvvm,这似乎是不必要的工作。话虽如此,如果它不会导致比我认真考虑的任何其他解决方案更多的工作。