2013-01-10 24 views
0

我綁定(至少我認爲我做到了)數據到ListBox後面的教程。我想綁定的類元素中包含數據,但在某些事件發生後,我在ListBox上看不到任何內容。我有以下部分XAML:listbox綁定不起作用,因爲它應該

<ListBox x:Name="jukeBoxListBox" Height="227" VerticalAlignment="Top" ItemsSource="{Binding FilePathList}"/> 

我在WPF窗體cs文件中。我應該設置爲課程FolderItems還是其attr filePathList?我也應該使用ObservableCollection而不是list

InitializeComponent();  
FolderItems folderItems = new FolderItems(); 
this.DataContext = folderItems.FilePathList; 

我的數據類:

class FolderItems : INotifyPropertyChanged 
{ 
    #region INotifyPropertyChanged implementation 

    public event PropertyChangedEventHandler PropertyChanged; 

    protected void Notify(string propertyName) 
    { 
     if (this.PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
    #endregion INotifyPropertyChanged implementation 

    private ObservableCollection<String> _pathList = new ObservableCollection<string>(); 

    public ObservableCollection<String> FilePathList 
    { 
     get { return _pathList; } 
     set 
     { 
      if (value != _pathList) 
      { 
       _pathList = value; 
       Notify("FilePathList"); 
      } 
     } 
    } 
} 

我想我需要一提的是我改變一個Button click事件的List元素。也許下面是問題的一部分。

//in the event fItems is an instance of FolderItems 
var files = new ObservableCollection<string>(); 
ProcessFiles(of.SelectedPath, files); 
fItems.FilePathList = files; 
//... 

    private void ProcessFiles(string path, ICollection<string> files) 
    { 
     foreach (var file in Directory.GetFiles(path).Where(name => name.EndsWith(".mp3", StringComparison.OrdinalIgnoreCase))) 
      files.Add(file);  

     foreach (var directory in Directory.GetDirectories(path)) 
      ProcessFiles(directory, files); 

    } 

我來自Javaland,是全新的C#。請原諒我的語言。

+0

您在循環目錄時忽略了調用'ProcessFiles'返回的集合。您應該將集合作爲方法參數傳遞給'ProcessFiles',並將文件名添加到始終相同的集合中。 – Clemens

回答

2

如果您將List<string>更改爲ObservableCollection<string>see here),您的綁定會收到關於列表中的更改的通知,例如,當你添加項目。

另外,您必須將通知調用中的屬性名稱更改爲filePathList

而且,您應該遵循.Net中的屬性編碼約定,這些約定通常使用大寫的大寫字符來編寫。所以你的財產將是FilePathList

private ObservableCollection<String> pathList = new ObservableCollection<string>(); 

public ObservableCollection<String> FilePathList 
{ 
    get { return pathList; } 
    set 
    { 
     if (value != pathList) 
     { 
      pathList = value; 
      Notify("FilePathList"); // changed here 
     } 
    } 
} 

更改爲重命名屬性的綁定:

<ListBox ... ItemsSource="{Binding FilePathList}"/> 

Binding to CollectionsUsing Collection Objects as a Binding Source見。


UPDATE

,如下所示,使遞歸你ProcessFiles方法應被寫入。

private void ProcessFiles(string path, ICollection<string> files) 
{ 
    foreach (var file in Directory.GetFiles(path).Where(name => name.EndsWith(".mp3", StringComparison.OrdinalIgnoreCase))) 
    { 
     files.Add(file); 
    } 

    foreach (var directory in Directory.GetDirectories(path)) 
    { 
     ProcessFiles(directory, files); 
    } 
} 

而且這樣調用:

var files = new ObservableCollection<string>(); 
ProcessFiles(of.SelectedPath, files); 

var folderItems = new FolderItems(); 
folderItems.FilePathList = files; 
DataContext = folderItems; 

,或者您需要更高版本(也許在一些事件處理程序)來訪問FolderItems對象,你可能會得到它從DataContext回:

DataContext = new FolderItems(); 

... 

var folderItems = DataContext as FolderItems; 
ProcessFiles(of.SelectedPath, folderItems.FilePathList); 
+0

我試過這些,但沒有發生在列表框中。我編輯了問題到最新的形式。 – mechanicum

+0

完成更改。 'fItems.FilePathList = files;'填充FilePathList,但是沒有任何東西出現在列表框中。 – mechanicum

+0

您是否將'fItems'分配給MainWindow的'DataContext'?在你的問題中,你正在創建一個名爲'folderItems'的東西。 – Clemens

0

試試這個。 我搜索的MP3文件保證的MP3。你可以根據需要改變模式。

 public class FolderItems : INotifyPropertyChanged 
     { 

      public event PropertyChangedEventHandler PropertyChanged; 



      //Temp Data 
      public FolderItems() 
      { 


    //Add System.windows.Form assampbly. 
      var dialog = new System.Windows.Forms.FolderBrowserDialog(); 
      System.Windows.Forms.DialogResult result = dialog.ShowDialog(); 

      if (result == System.Windows.Forms.DialogResult.OK) 
      { 
       FilePathList = ProcessFiles(dialog.SelectedPath); 


       ////var directories = new System.IO.DirectoryInfo("C:\\Windows\\").GetFiles().Select(x => x.Name); 

       //foreach (var file in directories) 
       //{ 
       // FilePathList.Add(file); 
       //} 
    } 
      } 

      private ObservableCollection<String> ProcessFiles(string path) 
      { 
       string[] directories; 
       ObservableCollection<String> fileList = new ObservableCollection<string>(); 
       var files = new System.IO.DirectoryInfo(path).GetFiles("*.dll").Select(x => x.Name); //Directory.GetFiles(path).Where(name => name.EndsWith(".mp3", StringComparison.OrdinalIgnoreCase)); 
       fileList = new ObservableCollection<String>(files.ToList<String>()); 


       //your processing further 
       //directories = Directory.GetDirectories(path); 
       //foreach (string directory in directories) 
       //{ 
       // // Process each directory recursively 
       // ProcessFiles(directory); 
       //} 

       return fileList; 
      } 

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

      private ObservableCollection<String> _pathList = new ObservableCollection<string>(); 

      public ObservableCollection<String> FilePathList 
      { 
       get { return _pathList; } 
       set 
       { 
        if (value != _pathList) 
        { 
         _pathList = value; 
         Notify("FilePathList"); 
        } 
       } 
      } 

     } 
+0

「ProcessFiles」方法位於WPF代碼中。關鍵是讓用戶選擇一個文件夾(從WPF窗口)。從選定的文件夾中獲取文件,然後將列表設置到'FolderItems'類中,該類將有一個ObservableCollection綁定到wpf部分的列表框。 – mechanicum

+0

@Jodha遞歸到子目錄中去哪裏? – Clemens

+0

我再次檢查,這兩個'fItems.FilePathList =文件;'正確設置。文件,充滿了33個有用的對象,也是fItems.FilePathList也是一樣的。我可以只做'someListBox.ItemSource = whatever',並獲取列表框來顯示東西,但我試圖分開視圖,並在學習綁定WPF風格的方式。這是可能的wpf權利? – mechanicum

相關問題