0
我有一个对象存储在数据库中,我在viewmodel中检索并放入可观察集合中。这些对象是每个都有一个名为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);
}
}
你如何更新你的图片?为什么只是'一个视图模型'? – NSGaga 2012-04-17 21:52:56
我更新包含以下属性的observablecollection:CurrentProperty.Images.Add(NewImage),然后将集合保存到数据库。该程序实际上并不需要mvvm,使用它的唯一原因是利用observablecollections来对列表框进行排序和重新排序。我确信这可以在一个视图模型中完成,如果我没有充分利用mvvm,这似乎是不必要的工作。话虽如此,如果它不会导致比其他解决方案更多的工作,我会认真考虑它。 – randomalbumtitle 2012-04-17 22:02:27