C# listbox绑定无法正常工作
在一个教程之后,我将数据绑定到一个C# listbox绑定无法正常工作,c#,wpf,listbox,observablecollection,C#,Wpf,Listbox,Observablecollection,在一个教程之后,我将数据绑定到一个列表框中(至少我认为是这样)。我想要绑定的class元素中有数据,但在某个事件之后,我在列表框中看不到任何内容。我有以下部分XAML: <ListBox x:Name="jukeBoxListBox" Height="227" VerticalAlignment="Top" ItemsSource="{Binding FilePathList}"/> 我的数据类: class FolderItems : INotifyPropertyChange
列表框中(至少我认为是这样)。我想要绑定的class元素中有数据,但在某个事件之后,我在列表框中看不到任何内容。我有以下部分XAML:
<ListBox x:Name="jukeBoxListBox" Height="227" VerticalAlignment="Top" ItemsSource="{Binding 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");
}
}
}
}
我来自爪哇岛,是C#的新手。请原谅我的语言。如果您将列表
更改为ObservableCollection
(),您的绑定将收到列表中更改的通知,例如,当您添加项目时
此外,您必须将Notify调用中的属性名称更改为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
}
}
}
您应该遵循.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
}
}
}
被这样称呼:
var files = new ObservableCollection<string>();
ProcessFiles(of.SelectedPath, files);
var folderItems = new FolderItems();
folderItems.FilePathList = files;
DataContext = folderItems;
试试这个。
我搜索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");
}
}
}
}
公共类文件夹项:INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
//温度数据
公共福利项目()
{
//添加System.windows.Form可攻击性。
var dialog=new System.Windows.Forms.FolderBrowserDialog();
System.Windows.Forms.DialogResult=dialog.ShowDialog();
if(result==System.Windows.Forms.DialogResult.OK)
{
FilePathList=ProcessFiles(dialog.SelectedPath);
////var directories=new System.IO.DirectoryInfo(“C:\\Windows\\”).GetFiles()。选择(x=>x.Name);
//foreach(目录中的var文件)
//{
//FilePathList.Add(文件);
//}
}
}
私有ObservableCollection进程文件(字符串路径)
{
字符串[]目录;
ObservableCollection文件列表=新的ObservableCollection();
var files=new System.IO.DirectoryInfo(path).GetFiles(“*.dll”).Select(x=>x.Name);//Directory.GetFiles(path).Where(Name=>Name.EndsWith(“.mp3”,StringComparison.OrdinalIgnoreCase));
fileList=newobserveCollection(files.ToList());
//请继续处理
//目录=Directory.GetDirectories(路径);
//foreach(目录中的字符串目录)
//{
////递归地处理每个目录
//进程文件(目录);
//}
返回文件列表;
}
受保护的无效通知(字符串propertyName)
{
if(this.PropertyChanged!=null)
{
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
}
私有ObservableCollection_路径列表=新ObservableCollection();
公共ObservableCollection文件路径列表
{
获取{return\u pathList;}
设置
{
如果(值!=\u路径列表)
{
_路径列表=值;
通知(“文件路径列表”);
}
}
}
}
我尝试了这些,但列表框中什么也没发生。我将问题编辑为最新形式。已完成更改<代码>fItems.FilePathList=文件
填充文件路径列表,但列表框上没有显示任何内容。您是否将fItems
分配给主窗口的DataContext
?在你的问题中,你正在创建一个名为folderItems
的东西。就在下面,InitializeComponent()
有folderItems folderItems=new folderItems();this.DataContext=folderItems.FilePathList代码>啊,这里还有一个问题:您必须将DataContext
设置为folderItems
,而不是folderItems.FileListPath
。在调试器中运行程序时,应该在Visual Studio输出窗口中看到绑定错误消息。查看我的编辑当在目录上循环时,您正在忽略调用ProcessFiles
返回的集合。您应该将集合作为方法参数传递给ProcessFiles
,并向始终相同的集合添加文件名。ProcessFiles
方法位于WPF代码中。关键是让用户选择一个文件夹(从WPF窗口)。从所选文件夹中获取文件,然后将列表设置到FolderItems
类中,该类将有一个observeCollection
绑定到wpf部分的列表框。@Jodha子目录的递归在哪里?我再次选中该fItems.FilePathList=files代码>在两侧正确设置。文件,充满了33个有用的对象,也是fItems.FilePathList也一样。我可以只做someListBox.ItemSource=which
,让listbox显示内容,但我正在尝试分离视图,同时学习绑定wpf样式。这在wpf中是可能的,对吗?我根据克莱门斯的建议修复了代码。我不认为这是递归,因为我选择了一个没有文件夹的文件夹,里面只有mp3文件。我可以在上面的每个变量中看到文件,但不能在列表框中看到:D
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;
DataContext = new FolderItems();
...
var folderItems = DataContext as FolderItems;
ProcessFiles(of.SelectedPath, folderItems.FilePathList);
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");
}
}
}
}