在我的项目中,我有一个视图,用户可以在其中看到具有子文件夹和图像(通过 MVVM 和模型设置)的 ImageCollections。我想要实现的功能之一是一个小图像保持器,其中显示下一个尚未看到的图像。我已经在下面的 ImageCollection 模型中设置了它,它可以工作,但是:它不会实时更新。
我的意思是,当我单击正在查看的图像时,ShowNextToWatchImage 图像不会在视图中自动更新(如果我退出应用程序并重新启动它或重新打开文件夹时,它会显示正确的新图像)。
我怎样才能解决这个问题?
在模型中:
public Image ShowNextToWatchImage
{
get
{
var found = false;
Image nextToWatchImage = null;
foreach (var folder in Folders)
{
foreach (var image in folder.Images)
{
if (!image.ImageHasBeenViewed)
{
nextToWatchImage = image;
found = true;
break;
}
nextToWatchImage = image;
}
if (found)
{
break;
}
}
return nextToWatchImage;
}
set
{
_showNextToWatchImage = value;
NotifyOfPropertyChange(() => ShowNextToWatchImage);
}
}
编辑:
在尝试实施 Sheridan 的回答之后,我得到了以下内容(我确信我以某种方式搞砸了),这给了我任何东西(或者至少在该项目的视图中没有显示任何内容。
public class Collection : PropertyChangedBase
{
private Image _showNextToWatchImage;
public Collection()
{
Folders = new List<Folder>();
}
public Image ShowNextToWatchImage
{
get { return _showNextToWatchImage; }
set
{
_showNextToWatchImage = value;
UpdateNextToWatchImage();
NotifyOfPropertyChange(() => ShowNextToWatchImage);
}
}
public void UpdateNextToWatchImage()
{
var found = false;
Image nextToWatchImage = null;
foreach (var folder in Folders)
{
foreach (var image in folder.Images)
{
if (!image.ImageHasBeenViewed)
{
nextToWatchImage = image;
found = true;
break;
}
nextToWatchImage = image;
}
if (found)
{
break;
}
}
ShowNextToWatchImage = nextToWatchImage;
}
public List<Folder> Folders { get; set; }
}