徘徊如果我可以得到帮助,组合框列出hdds,点击时我希望列表框列出来自相应的hdd的位图图像缩略图(每个选定的硬盘转换为字符串,我希望用作获取缩略图的地址,我最初帮助让这个只用完整的图像,但因为那很慢我正在尝试使用缩略图,到目前为止我没有错误,但列表框中没有任何显示我确定我已经绑定
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
private void Window_Loaded(object sender, RoutedEventArgs e) //Window Loaded Event
{
///Load Avaliable Drives Into ComboBox
string[] drives = Environment.GetLogicalDrives(); //Drive Letters, Into A String Array
foreach (string drive in drives)
{
HDDSelectionBox.Items.Add(drive); //Adds Each Drive Letter As A Combox Box Item
}
string root = (HDDSelectionBox.SelectedItem.ToString()); //Contains Directory Path For Images
string[] supportedExtensions = new[] { ".bmp", ".jpeg", ".jpg", ".png", ".tiff" };
var files = System.IO.Directory.EnumerateFiles(root, "*.*").Where(s => supportedExtensions.Contains(System.IO.Path.GetExtension(s).ToLower()));
List<Photos> images = new List<Photos>();
if (HDDSelectionBox.SelectedItem != null) //If a item has been selected
{
foreach (var file in files)
{
Photos id = new Photos()
{
Path = file,
FileName = System.IO.Path.GetFileName(file),
Extension = System.IO.Path.GetExtension(file)
};
BitmapImage img = new BitmapImage();
img.BeginInit();
img.CacheOption = BitmapCacheOption.OnLoad;
img.UriSource = new Uri(file, UriKind.Absolute);
img.EndInit();
id.Width = img.PixelWidth;
id.Height = img.PixelHeight;
// I couldn't find file size in BitmapImage
FileInfo fi = new FileInfo(file);
id.Size = fi.Length;
images.Add(id);
}
ImageListBox.ItemsSource = images;
}
}
}
public class Photos
{
/// <summary>
/// A name for the image, not the file name.
/// </summary>
public string Name { get; set; }
/// <summary>
/// A description for the image.
/// </summary>
public string Description { get; set; }
/// <summary>
/// Full path such as c:\path\to\image.png
/// </summary>
public string Path { get; set; }
/// <summary>
/// The image file name such as image.png
/// </summary>
public string FileName { get; set; }
/// <summary>
/// The file name extension: bmp, gif, jpg, png, tiff, etc...
/// </summary>
public string Extension { get; set; }
/// <summary>
/// The image height
/// </summary>
public int Height { get; set; }
/// <summary>
/// The image width.
/// </summary>
public int Width { get; set; }
/// <summary>
/// The file size of the image.
/// </summary>
public long Size { get; set; }
}
}
1 回答
首先创建一个可用于执行属性通知的基本模型(XAML控件需要这样才能响应更改:
现在创建一个枚举硬盘和图像并创建缩略图的模型:
显然,您需要创建此类的实例并将其分配给Windows的DataContext:
最后将您的ComboBox和ListBox添加到窗口,设置绑定并覆盖ListBox项模板以显示图像: