0
我想將目錄中的文件列表綁定到列表框。Winforms Listbox數據綁定到目錄中的文件列表
這裏是我想要的代碼片段,我嘗試到目前爲止,lstFiles是一個ListBox,我想要將Files屬性綁定到。但是ListBox是空的。請幫忙。
public partial class Form1 : Form, INotifyPropertyChanged
{
private IList<FileInfo> _files = new List<FileInfo>();
public IList<FileInfo> Files
{
get
{
return this._files;
}
set
{
if (value != this._files)
{
this._files = value;
NotifyPropertyChanged("Files");
}
}
}
public Form1()
{
InitializeComponent();
lstFiles.DataSource = Files;
lstFiles.DataBindings.Add("Name", Files, "Files");
lstFiles.DisplayMember = "Name";
}
private void btnStartPath_Click(object sender, EventArgs e)
{
var dialog = new FolderBrowserDialog();
var result = dialog.ShowDialog();
if (result == DialogResult.OK)
{
txtStartPath.Text = dialog.SelectedPath;
}
}
private void btnFind_Click(object sender, EventArgs e)
{
Files = new DirectoryInfo(txtStartPath.Text).EnumerateFiles().ToList();
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
#endregion
}
這是一個WinForms應用程序,用戶從UI的期望路徑並且點擊查找按鈕,獲取文件的列表。我正在使用DirectoryInfo(txtStartPath.Text).EnumerateFiles()來獲取文件添加到Files屬性 – billboard