0

我将(至少我认为我做到了)数据绑定到了ListBox以下教程。我想要绑定的类元素中有数据,但ListBox在某些事件之后我什么也看不到。我有以下部分 XAML:

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

在我拥有的 WPF 表单 cs 文件中。我应该设置为 classFolderItems还是它的 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");
            }
        }
    }
}

我想我需要提到的是我更改了点击事件List中的元素。Button也许以下是问题的一部分。

//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# 的新手。请原谅我的语言。

4

2 回答 2

2

如果您更改List<string>ObservableCollection<string>请参阅此处),您的绑定将收到有关列表更改的通知,例如当您添加项目时。

此外,您必须将 Notify 调用中的属性名称更改为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}"/>

另请参阅绑定到集合使用集合对象作为绑定源


更新

您的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);
于 2013-01-10T09:54:51.483 回答
0

试试这个。我搜索 dll 文件而不是 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");
                    }
                }
            }

        }
于 2013-01-10T12:12:38.900 回答