2016-08-06 8 views
0

私はリストボックスに表示される画像を持つフォルダを持っています。 ObservableCollectionはその画像を保存します。私のプログラムでは、ユーザは選択された画像をトリミングし、トリミング後にその画像を保存することができます。問題は、画像を保存した後、ObservableCollectionが更新されず(OnPropertyChangeイベントが発生しても)、同じ画像が表示されることです。誰も同じ問題を抱えていますか?ObservableCollectionを更新するには?

EDIT

私は私のObservableCollectionを配置BindingManagerを持っています。 XAMLファイルで

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

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

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> 
+1

あなたは何をしているのか理解して問題を解決するためのコードを記入してください。 –

+0

ObservableCollectionsにはすでにINotifyChangedインターフェイスが含まれているため、2度インパクトする必要はなく、問題が発生する可能性があります。 –

+0

INotifyPropertyChangedインターフェイスを削除しても、同じ問題が発生する – Anton

答えて

3

私はバインディングがあなたの主な問題はないと思います。イメージのキャッシュを確認する必要があります。お試しください:Reloading an image in wpf

関連する問題