0

在我的项目中,我有一个视图,用户可以在其中看到具有子文件夹和图像(通过 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; }
    }
4

2 回答 2

1

您需要NotifyOfPropertyChange在值更改时调用,但您实际上是在更改 getter 而不是 setter 中的值...尝试创建一个名为的新方法UpdateNextImage并将代码放入其中以确定下一个Image

public Image ShowNextToWatchImage
{
    get
    {
        return _showNextToWatchImage;
    }
    set
    {
        _showNextToWatchImage = value;
        UpdateNextImage();
        NotifyOfPropertyChange(() => ShowNextToWatchImage);
    }
}

更新>>>

我仍然不能 100% 确定您的设置(最好能向我们展示您的所有相关代码),但您说您希望在单击Image. 这让我觉得你在Command某处有点击或处理程序。如果这是正确的,那么我建议您将下一个选择功能放在那里:

private void Image_Click(object sender, RoutedEventArgs e)
{
    ShowNextToWatchImage = GetNextImage();
}

private Image GetNextImage()
{
    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;        
}
于 2013-10-03T10:16:34.567 回答
0

您是否在 XAML 中设置mode=TwoWay了绑定?所以:{Binding ShowNextToWatchImage, Mode=TwoWay}

于 2013-10-03T11:21:54.950 回答