如何更新ObservableCollection?

时间:2016-08-06 13:53:12

标签: c# wpf image listbox observablecollection

我有图片文件夹,我在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>

1 个答案:

答案 0 :(得分:3)

我认为绑定不是你的主要问题。你应该检查图像缓存。尝试:Reloading an image in wpf

相关问题