实现可观察集合的问题

时间:2012-04-17 21:30:49

标签: c# wpf binding mvvm observablecollection

我有一个存储在数据库中的对象,我在viewmodel中检索并放入一个可观察的集合中。这些对象是属性(house / realestate),每个属性都有一个名为Images的子对象。每个属性可以有多个图像(但每个图像只能有一个属性)。我只想使用一个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);
        }
    }    

1 个答案:

答案 0 :(得分:0)

按照这个问题提供的答案结束解决问题:

Listview to display ObservableSelection

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

相关问题