2013-01-10 25 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

相关问题