0

I have folder with images, which I show in my ListBox. ObservableCollection stores that images. In my program users could crop selected image and after cropping save that image. Problem is that after saving the image my ObservableCollection is not updated (although OnPropertyChange event is rise) and shows the same image. Does anybody have the same problem?

EDIT

I have BindingManager where I place my 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));
    }
}

In Xaml file I bind source to PhotoList:

 <ListView x:Name="PhotoListView" ItemsSource="{Binding PhotoList, UpdateSourceTrigger=PropertyChanged}">

After double click on image, image will be opened in new window where user can crop it and save to DefaultDestFolder which is tracking by FileSystemWatcher:

     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;
    }

And on change event i update my ObservableCollection but it doesn't work:

   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
                });
            }));
        }          
    }

EDIT

    <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>
Ant
  • 129
  • 1
  • 11

1 Answers1

3

I think binding isn't your main problem. You should check image caching. Try: Reloading an image in wpf

Community
  • 1
  • 1
Mateusz
  • 194
  • 7