1

我有一个列表框,它绑定到从隔离存储中获取的图像,这是我获取这些图像的方式...

     foreach (string fileName in fileStorage.GetFileNames("images//*.*"))
            {
                if (fileName == null)
                    break;
                string filepath = System.IO.Path.Combine("images", fileName);
                using (IsolatedStorageFileStream imageStream = fileStorage.OpenFile(filepath, FileMode.Open, FileAccess.Read))
                {
                    var imageSource = PictureDecoder.DecodeJpeg(imageStream);
                    BitmapImage bitmapImage = new BitmapImage();
                    bitmapImage.SetSource(imageStream);
                    vltBitmapImage.Add(bitmapImage);

                }
            }
          this.vaultbox.ItemsSource = vltBitmapImage;

现在我想删除选定的图像(可以选择多个)

     if (vaultbox.SelectedItems.Count != 0)
        {
            MessageBoxResult m = MessageBox.Show("Files will be deleted forever", "Are you sure?", MessageBoxButton.OKCancel);
            if (m == MessageBoxResult.Cancel)
                vaultbox.SelectedIndex = -1;
            else if(m==MessageBoxResult.OK)
            {
                foreach (BitmapImage item in vaultbox.SelectedItems)
                {
                    //what should i do here?
                }
            }
        }
        else
            MessageBox.Show("No file selected");
4

1 回答 1

0

您必须存储连接图像文件名。

例如:

public class ImageToName
{
   public BitmapImage Image {get;set;}
   public string FileName {get;set;}
}

列表将更改为:

List<ImageToName> vltBitmapImage = new List<ImageToName>();

并以这种方式填写列表:

 //read file
 var imageSource = PictureDecoder.DecodeJpeg(imageStream);
 BitmapImage bitmapImage = new BitmapImage();
 bitmapImage.SetSource(imageStream);
 var item = new ImageToName {Image = bitmapImage , Name = fileName };
 vltBitmapImage.Add(item);

并且您需要更改ListBox声明

 <ListBox.ItemTemplate>
   <DataTemplate>
   <Image Source={Binding Image}/>
   </DataTemplate>
 </ListBox.ItemTemplate>

因此,您将能够从选定的项目中获取文件名。

于 2012-10-07T16:45:22.117 回答