列表框绑定不能正常工作

本文关键字:工作 常工作 绑定 不能 列表 | 更新日期: 2023-09-27 17:51:08

我将(至少我认为我做过)数据绑定到ListBox下面的教程。我想要绑定的类元素中有数据,但在某些事件后,我在ListBox上看不到任何东西。我有以下部分XAML:

 <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;

My data class:

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单击事件中更改了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);
    }

我来自爪哇,是c#的新手。请原谅我的语言。

列表框绑定不能正常工作

如果你将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);

试试这个。我搜索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");
                    }
                }
            }
        }