实现可观察收集的问题
本文关键字:问题 观察 实现 | 更新日期: 2023-09-27 18:31:30
我有一个存储在数据库中的对象,我正在我的视图模型中检索该对象并将其放入可观察的集合中。这些对象是属性(房屋/房地产),每个属性都有一个名为 Images 的子对象。每个属性可以有多个图像(但每个图像只能有一个属性)。我只想使用一个视图模型。我有填充列表框的属性很好,我可以成功地将图像绑定到后续列表框,但前提是我通过 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);
}
}
最终按照此问题提供的答案解决了该问题:
用于显示"可观察选择"的列表视图
我为图像创建了一个可观察集合,然后创建了一个新方法 images_Update(),每次视图(属性可观察集合)的当前项更改时都会调用该方法。我还将其插入AddImage()和DeleteImage()方法的底部,以确保在调用它们时更新列表。