I have a listView which loads images from a directory using the 'ImageViewCollectionModel' along with the 'FileViewModel' .
I have 2 button, one button to 'Delete' the selected image and another button to 'View' the selected image.
When I want to delete, I want to delete from the ListView and also from the directory. Please help me to remove the selected item from the list & also from
the directory. Thank you.
ListView:
<ListView SelectionMode="Single" x:Name="ListViewImage" Width="Auto" SelectedItem="{Binding ImageFileName}"
ItemsSource="{Binding Path=AllImages}" Margin="0,20,0,0"
VirtualizingStackPanel.IsVirtualizing="True"
VirtualizingStackPanel.VirtualizationMode= "Recycling" Height="333" VerticalAlignment="Top"><!--<ListView.ItemContainerStyle><Style TargetType="{x:Type ListViewItem}"><Setter Property="ToolTip" Value="{Binding Path=Thumbnail}" /></Style></ListView.ItemContainerStyle>--><ListView.ItemTemplate><DataTemplate><DataTemplate.Resources><Storyboard x:Key="WaitingTimeline" Timeline.DesiredFrameRate="10"><DoubleAnimationUsingKeyFrames BeginTime="00:00:00" RepeatBehavior="Forever"
Storyboard.TargetName="WaitingImage"
Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0]
.(RotateTransform.Angle)"><SplineDoubleKeyFrame KeyTime="00:00:00" Value="-15"/><SplineDoubleKeyFrame KeyTime="00:00:03" Value="15"/></DoubleAnimationUsingKeyFrames></Storyboard></DataTemplate.Resources><StackPanel Orientation="Horizontal" Height="100"><Border BorderBrush="GhostWhite" BorderThickness="2" ><Image x:Name="ThumbnailImage" Margin="2" Visibility="Collapsed" Height="150" Width="150" Source="{Binding Path=Thumbnail}" ></Image></Border><Image x:Name="WaitingImage" Visibility="Visible" Height="20" Width="20" Source="./Hourglass.png"><Image.RenderTransform><TransformGroup><RotateTransform Angle="0" CenterX="10" CenterY="10"/></TransformGroup></Image.RenderTransform></Image><TextBlock Margin="10,40,0,0" FontStyle="Italic" FontWeight="SemiBold" Foreground="#1432AA" Text="{Binding Path=ShortName}" /></StackPanel><DataTemplate.Triggers><DataTrigger Binding="{Binding Path=IsLoaded}" Value="True"><Setter Property="Visibility" TargetName="ThumbnailImage" Value="Visible"/><Setter Property="Visibility" TargetName="WaitingImage" Value="Collapsed"/></DataTrigger><DataTrigger Binding="{Binding Path=IsLoaded}" Value="False"><Setter Property="Visibility" TargetName="WaitingImage" Value="Visible"/><Setter Property="Visibility" TargetName="ThumbnailImage" Value="Collapsed"/><DataTrigger.EnterActions><BeginStoryboard x:Name="WaitingTimeline_BeginStoryboard" Storyboard="{StaticResource WaitingTimeline}"/></DataTrigger.EnterActions><DataTrigger.ExitActions><StopStoryboard BeginStoryboardName="WaitingTimeline_BeginStoryboard"/></DataTrigger.ExitActions></DataTrigger></DataTemplate.Triggers></DataTemplate></ListView.ItemTemplate></ListView><Button Content="Edit" HorizontalAlignment="Left" Margin="73,380,0,0" VerticalAlignment="Top" Height="150" Width="150" Name="Edit_Photo" Click="Edit_Photo_Click"/><Button Content="Delete" HorizontalAlignment="Left" Margin="475,380,0,0" VerticalAlignment="Top" Height="150" Width="150" Name="Delte_Photo" Click="Delte_Photo_Click"/><Label Name="lImageName" Margin="228,448,234,82" HorizontalContentAlignment="Center" Foreground="ForestGreen" FontWeight="Bold" Content="{Binding SelectedItem.ShortName, ElementName=ListViewImage}" >C# Code:
public ImggLList()
{
InitializeComponent();
ListViewImage.Items.Clear();
DataContextChanged += OnDataContextChanged;
string destination_dir = System.IO.Directory.GetCurrentDirectory() + @"./4x6";
ImageFileCollectionViewModel ImagesViewModel = new ImageFileCollectionViewModel();
ImageFileControler.CompleteViewList(ImagesViewModel, destination_dir);
ListViewImage.DataContext = ImagesViewModel;
}
private ImageFileCollectionViewModel _currentDataContext = null;
private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (_currentDataContext == DataContext) return;
if (_currentDataContext != null)
_currentDataContext.SelectedImageFileViewModels = null;
_currentDataContext = DataContext as ImageFileCollectionViewModel;
if (_currentDataContext != null)
_currentDataContext.SelectedImageFileViewModels = ListViewImage.SelectedItems;
}
public string getImageName { get; set; }
private void Edit_Photo_Click(object sender, RoutedEventArgs e)
{
//getImageName = lImageName.Content.ToString();
var openImageEditingWindow = new PhotoView(this);
openImageEditingWindow.ShowDialog();
}
private List<ImageFileViewModel> copyOfSelection;
private ImageFileCollectionViewModel imageFileCollection;
private void Delte_Photo_Click(object sender, RoutedEventArgs e)
{
copyOfSelection = imageFileCollection.SelectedImageFileViewModels.Cast<ImageFileViewModel>().ToList();
foreach (ImageFileViewModel ifvm in copyOfSelection)
{
copyOfSelection.Remove(ifvm);
}
}
}ImageFileCollectionViewModel.cs
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using ImagePrintUtility.Model;
namespace ImagePrintUtility.ViewModel
{
public class ImageFileCollectionViewModel: INotifyPropertyChanged
{
private ObservableCollection<ImageFileViewModel> _allImages;
private int dataItemsCount;
public ObservableCollection<ImageFileViewModel> AllImages
{
get { return _allImages; }
}
public int DataItemsCount
{
get
{
return dataItemsCount;
}
private set
{
dataItemsCount = value;
OnPropertyChanged("DataItemsCount");
}
}
public ImageFileCollectionViewModel()
{
this._allImages = new ObservableCollection<ImageFileViewModel>();
this.DataItemsCount = 0;
}
public void AddNewPhotoItem(IImageFile imageFile)
{
ImageFileViewModel newImageFileViewModel = new ImageFileViewModel(imageFile);
this._allImages.Add(newImageFileViewModel);
this.DataItemsCount++;
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private IList _selectedImageFileViewModels;
public IList SelectedImageFileViewModels
{
get { return _selectedImageFileViewModels; }
set
{
if (_selectedImageFileViewModels != value)
{
_selectedImageFileViewModels = value;
OnPropertyChanged("SelectedImageFileViewModels");
}
}
}
}
FileViewModel.cs
using System.ComponentModel;
using System.IO;
using ImagePrintUtility.Model;
namespace ImagePrintUtility.ViewModel
{
public class FileViewModel: INotifyPropertyChanged
{
protected IImageFile _imageFile;
public string ShortName
{
get { return Path.GetFileName(_imageFile.FileName); }
}
public string FileName
{
get { return _imageFile.FileName; }
}
public FileViewModel(IImageFile imageFile)
{
this._imageFile = imageFile;
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}