2013-11-27 72 views
0

徘徊如果我可以得到幫助,單擊時組合框列出hdds我希望列表框列出來自相應hdd的位圖圖像縮略圖(每個選定的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

首先創建,你可以用做物業通知的基礎模型(T他XAML控件需要這一點是爲了應對變化:

public abstract class ObservableObject : INotifyPropertyChanged 
{ 
    protected ObservableObject() 
    { 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    protected void OnPropertyChanged<T>(Expression<Func<T>> propertyExpresion) 
    { 
     var property = (MemberExpression)propertyExpresion.Body; 
     this.OnPropertyChanged(property.Member.Name); 
    } 

    protected void OnPropertyChanged(string propertyName) 
    { 
     if (this.PropertyChanged != null) 
     { 
      this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

    protected void SetValue<T>(ref T refValue, T newValue, Expression<Func<T>> propertyExpresion) 
    { 
     if (!object.Equals(refValue, newValue)) 
     { 
      refValue = newValue; 
      this.OnPropertyChanged(propertyExpresion); 
     } 
    } 

    protected void SetValue<T>(ref T refValue, T newValue, Action valueChanged) 
    { 
     if (!object.Equals(refValue, newValue)) 
     { 
      refValue = newValue; 
      valueChanged(); 
     } 
    } 

    protected void SetValue<T>(ref T refValue, string propertyName, T newValue) 
    { 
     if (!object.Equals(refValue, newValue)) 
     { 
      refValue = newValue; 
      this.OnPropertyChanged(propertyName); 
     } 
    } 
} 

現在創建,列舉你的硬盤驅動器和圖像並創建縮略圖的模式:

public class ImagesModel : ObservableObject 
{ 
    private ObservableCollection<string> _HardDrives; 
    public ObservableCollection<string> HardDrives 
    { 
     get {return this._HardDrives;} 
     set {this._HardDrives = value; OnPropertyChanged(() => this.HardDrives);} 
    } 

    private string _CurrentDrive; 
    public string CurrentDrive 
    { 
     get {return this._CurrentDrive;} 
     set { 
      this._CurrentDrive = value; 
      OnPropertyChanged(() => this.CurrentDrive); 

      // enumerate files in this drives 
      string[] supportedExtensions = new[] { ".bmp", ".jpeg", ".jpg", ".png", ".tiff" }; 
      this.Images = new ObservableCollection<PhotoModel>(
       System.IO.Directory.EnumerateFiles(value, "*.*") 
       .Where(s => supportedExtensions.Contains(System.IO.Path.GetExtension(s).ToLower())) 
       .Select(filename => new PhotoModel(filename))); 
     } 
    } 

    private ObservableCollection<PhotoModel> _Images; 
    public ObservableCollection<PhotoModel> Images 
    { 
     get {return this._Images;} 
     set {this._Images = value; OnPropertyChanged(() => this.Images);} 
    } 

    private PhotoModel _CurrentImage; 
    public PhotoModel CurrentImage 
    { 
     get {return this._CurrentImage;} 
     set {this._CurrentImage = value; OnPropertyChanged(() => this.CurrentImage);} 
    } 

    public ImagesModel() 
    { 
     this.HardDrives = new ObservableCollection<string>(Environment.GetLogicalDrives()); 
    } 
} 

public class PhotoModel : ObservableObject 
{ 
    private string _FileName; 
    public string FileName 
    { 
     get {return this._FileName;} 
     private set { this._FileName = value; OnPropertyChanged(() => this.FileName);} 
    } 

    private ImageSource _Image; 
    public ImageSource Image { 
     get {return this._Image;} 
     private set {this._Image = value;OnPropertyChanged(() => this.Image);} 
    } 

    public PhotoModel(string filename) 
    { 
     this.FileName = filename; 
     BitmapImage img = new BitmapImage(); 
     img.BeginInit(); 
     img.CacheOption = BitmapCacheOption.OnLoad; 
     img.UriSource = new Uri(filename, UriKind.Absolute); 
     img.DecodePixelWidth = 64; // or whatever 
     img.EndInit(); 
     this.Image = img; 
    } 
} 

顯然,你需要創建一個實例這個類和它分配給你的Windows的的DataContext:

public MainWindow() 
    { 
     InitializeComponent(); 
     this.DataContext = new ImagesModel(); 
    } 

最後你的組合框和列表框添加到窗口,設置您的綁定和覆蓋列表框項模板顯示圖像:

<Grid> 
    <ComboBox ItemsSource="{Binding HardDrives}" SelectedValue="{Binding CurrentDrive}" HorizontalAlignment="Left" Margin="27,35,0,0" VerticalAlignment="Top" Width="120"/> 
    <ListBox ItemsSource="{Binding Images}" SelectedValue="{Binding CurrentImage}" HorizontalAlignment="Left" Height="212" Margin="27,87,0,0" VerticalAlignment="Top" Width="188"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <Image Width="64" Height="64" Source="{Binding Image}" /> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 

</Grid> 
+0

哇那有點採取,但謝謝你,我會嘗試 –

+0

其實,這樣的XAML可以正確地綁定到你的代碼的大多數代碼都涉及到財產的通知..把所有這些東西都拿走,你會發現它其實很簡單。在真實世界的應用程序(比如數據庫應用程序)中,通常使用CastleProxy設置體系結構或創建包裝類,以便自動生成所有代碼。對於像這樣的基本東西,雖然很容易就可以根據需要隨時做,一旦得到了ObservableObject類。 –

+0

我只是新的建模,我慢慢掌握了綁定,資源和數據模板等想法,但havnt花了很多時間在模型和通知等,所以希望它不是很難讓我的頭周圍的模型的東西 –