我想直观地显示一个项目(在我的情况下是一个BitmapImage)已被添加到使用WPF/MVVM的ListBox中的集合。为了给出一些背景知识,我使用捕捉卡捕捉流式视频,并且在视频流式传输时拍摄静态图像。这些图像是通过单击UI上的“静止图像”按钮捕获的。捕捉图像后,我想要在UI上的单独面板中显示所述图像的缩略图。我明白需要做些什么才能做到这一点,但我似乎无法使其发挥作用。现在,我的模型执行所有数据检索。更新到ListBox绑定集合后刷新UI
public ObservableCollection<BitmapImage> GetAssetThumbnails()
{
var imageDir = ImgPath != null ? new DirectoryInfo(ImgPath) : null;
if(imageDir != null)
{
try
{
foreach (FileInfo imageFile in imageDir.GetFiles("*.jpg"))
{
var uri = new Uri(imageFile.FullName);
_assetThumbnails.Add(new BitmapImage(uri));
}
}
catch (Exception)
{
return null;
}
}
return _assetThumbnails;
}
我的视图模型建立在其构造模型的新实例,然后设置公共财产等于_assetModel.GetAssetThumbnails()
。 ViewModel具有所有标准的OnPropertyChanged事件和事件处理。
private void OnPropertyChanged(string propertyName)
{
if(PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#region Public Properties
public ObservableCollection<BitmapImage> AssetCollection
{
get { return _assetCollection; }
set
{
if (_assetCollection != value)
{
_assetCollection = value;
OnPropertyChanged("AssetCollection");
}
}
}
#endregion
private void _assetCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
OnPropertyChanged("AssetCollection");
}
public event PropertyChangedEventHandler PropertyChanged;
然后,在我的XAML文件中,我将ListBox的项目源绑定到AssetCollection。
<ListBox x:Name="lstBoxAssets" ItemsSource="{Binding AssetCollection}" Grid.Row="1" Background="{DynamicResource GrayColor}" Margin="5,0,0,5" ></ListBox>
我读过,集合中的每个项目都应该实现INotifyPropertyChanged接口。我试图创建一个实现该位图图像的包装类,但它也没有工作。有什么我在这里失踪?图像最初显示,但在图像被捕获和保存后不刷新。我有一种感觉,这是来自事件PropertyChanged
没有得到调用。我通过调试发现它在检查时始终为空,因此从不调用OnPropetyChanged
方法。我不知道我应该在哪里添加这个事件处理程序。我只使用代码隐藏文件将视图模型的数据上下文添加到视图,就是这样。这正是我通常想要添加任何事件处理的地方。任何人都可以看到简单的东西,我在这里失踪?
尝试“我试图创建一个包装类的位图图像实现,但它也没有工作。”再次。对项目的更改需要通过NotifyPropertyChanged从项目中获得。你有没有使用公共财产? – Paparazzi
是的,该物业是公共的。我会尝试再次创建一个包装类。我上次可能错过了一些东西。 – Zajn