列表框绑定不能正常工作

时间:2013-01-10 09:51:18

标签: c# wpf listbox observablecollection

我按照教程绑定(至少我认为我做过)数据到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;

我的数据类:

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

2 个答案:

答案 0 :(得分: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


更新

您的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);

答案 1 :(得分: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");
                    }
                }
            }

        }