如何更新ObservableCollection
本文关键字:ObservableCollection 更新 何更新 | 更新日期: 2023-09-27 18:01:03
我有一个包含图像的文件夹,我会将其显示在ListBox中。ObservableCollection存储这些图像。在我的程序中,用户可以裁剪选定的图像,裁剪后保存该图像。问题是,保存图像后,我的ObservableCollection不会更新(尽管OnPropertyChange事件上升(,并显示相同的图像。有人有同样的问题吗?
编辑
我在BindingManager中放置了observableCollection。
public class BindingsManager:INotifyPropertyChanged
{
private ObservableCollection<PhotoModel> _photoList;
public ObservableCollection<PhotoModel> PhotoList
{
get {return _photoList;}
set
{
_photoList = value;
OnPropertyChanged(nameof(PhotoList));
}
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
在Xaml文件中,我将源绑定到PhotoList:
<ListView x:Name="PhotoListView" ItemsSource="{Binding PhotoList, UpdateSourceTrigger=PropertyChanged}">
双击图像后,图像将在新窗口中打开,用户可以在新窗口中将其裁剪并保存到FileSystemWatcher:跟踪的DefaultDestFolder
private void WatchDestinationFolder()
{
var watcher = new FileSystemWatcher
{
Path = BindingsManager.DefaultsManager.DefaultDestFolder,
NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Size | NotifyFilters.LastAccess | NotifyFilters.LastWrite,
Filter = "*.*"
};
watcher.Changed += UpdatePhotoList;
watcher.EnableRaisingEvents = true;
}
在更改事件中,我更新了我的ObservableCollection,但它不起作用:
private void UpdatePhotoList()
{
var files = Directory.GetFiles(BindingsManager.DefaultsManager.DefaultDestFolder, "*.*");
BindingsManager.PhotoList = new ObservableCollection<PhotoModel>();
foreach (var file in files)
{
Application.Current.Dispatcher.Invoke((Action)(() =>
{
var fileInfo = new FileInfo(file);
BitmapImage img = new BitmapImage();
img.BeginInit();
img.CacheOption = BitmapCacheOption.OnLoad;
img.UriSource = new Uri(file, UriKind.Absolute);
img.EndInit();
BindingsManager.PhotoList.Add(new PhotoModel()
{
BitmapImage = img,
FullFileName = fileInfo.FullName,
ShortFileName = fileInfo.Name,
FileLastAccessTime = fileInfo.LastAccessTime,
FileSize = fileInfo.Length,
Width = (int)img.Width,
Height = (int)img.Height,
DirectoryName = fileInfo.DirectoryName,
FileCreationTime = fileInfo.CreationTime
});
}));
}
}
编辑
<ScrollViewer Grid.Column="2">
<ListView x:Name="PhotoListView" BorderThickness="0"
ItemsSource="{Binding PhotoList, UpdateSourceTrigger=PropertyChanged}">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<controls:Tile Style="{StaticResource TileStyle}">
<StackPanel Background="White" Width="190" Height="140" Orientation="Vertical">
<Image Margin="5" Width="180" Height="110" Stretch="Fill" Source="{Binding BitmapImage}"/>
<TextBlock Text="{Binding ShortFileName}" TextAlignment="Center" Height="20" FontStyle="Italic" FontWeight="ExtraLight" Foreground="Black"></TextBlock>
</StackPanel>
</controls:Tile>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<EventSetter Event="MouseDoubleClick" Handler="ItemMouseDoubleClick"></EventSetter>
<EventSetter Event="PreviewMouseLeftButtonUp" Handler="ItemMouseClick"></EventSetter>
</Style>
</ListView.ItemContainerStyle>
</ListView>
</ScrollViewer>
我认为绑定不是您的主要问题。您应该检查图像缓存。尝试:在wpf 中重新加载图像