You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C# WPF新手求助:无法在ListView表格中显示文件信息列表

问题解决:WPF ListView无法显示填充好的文件集合

问题根源

你的代码存在两个核心问题:

  1. allFileList是构造函数内的局部变量,WPF数据绑定无法访问局部变量,只能绑定到公共属性。
  2. 你把DataContext设为集合本身,但XAML里ItemsSource="{Binding allFileList}"是在找集合对象的allFileList属性,而ObservableCollection本身没有这个属性,导致绑定失效。

解决方案1:直接赋值ItemsSource(最简单,适合新手)

不需要用数据绑定,直接把集合赋值给ListView的ItemsSource属性:

修改后台构造函数最后一行:

// 替换原来的 lvFiles.DataContext = allFileList;
lvFiles.ItemsSource = allFileList;

同时XAML里可简化为:

<ListView Name="lvFiles">
    <ListView.View>
        <GridView>
            <GridViewColumn Width="160" Header="File Name" DisplayMemberBinding="{Binding FileName}"/>
            <GridViewColumn Width="160" Header="File Path" DisplayMemberBinding="{Binding FilePath}"/>
            <GridViewColumn Width="160" Header="Date Created" DisplayMemberBinding="{Binding DateCreated}"/>
            <GridViewColumn Width="150" Header="File Size" DisplayMemberBinding="{Binding FileSize}"/>
        </GridView>
    </ListView.View>
</ListView>

解决方案2:使用数据绑定(符合WPF规范)

把集合改成类的公共属性,设置正确的DataContext:

  1. 修改后台代码,添加公共属性:
public partial class FilePanel : UserControl
{
    // 添加公共属性,遵循C#命名规范(首字母大写)
    public ObservableCollection<FileList> AllFileList { get; set; }

    public class FileList
    {
        public string FileName { get; set; }
        public string FilePath { get; set; }
        public string DateCreated { get; set; }
        public string FileSize { get; set; }
    }

    // ... 其他代码保持不变 ...

    public FilePanel()
    {
        InitializeComponent();

        // 初始化公共属性
        AllFileList = new ObservableCollection<FileList>();

        var searchPath = @"C:\Users\";
        FileInfo[] files = new DirectoryInfo(searchPath).GetFiles("*", new EnumerationOptions
        {
            IgnoreInaccessible = true,
            RecurseSubdirectories = true
        });

        foreach (FileInfo file in files)
        {
            var fileSize = FileSizeFormatter.FormatSize(file.Length);
            AllFileList.Add(new FileList
            {
                FileName = file.Name,
                FilePath = file.DirectoryName,
                DateCreated = file.CreationTime.ToShortDateString(),
                FileSize = fileSize
            });
        }

        // 设置DataContext为当前控件实例,让绑定能找到公共属性
        DataContext = this;
    }
}
  1. 修改XAML的ItemsSource绑定路径:
<ListView Name="lvFiles" ItemsSource="{Binding AllFileList}">
    <ListView.View>
        <GridView>
            <GridViewColumn Width="160" Header="File Name" DisplayMemberBinding="{Binding FileName}"/>
            <GridViewColumn Width="160" Header="File Path" DisplayMemberBinding="{Binding FilePath}"/>
            <GridViewColumn Width="160" Header="Date Created" DisplayMemberBinding="{Binding DateCreated}"/>
            <GridViewColumn Width="150" Header="File Size" DisplayMemberBinding="{Binding FileSize}"/>
        </GridView>
    </ListView.View>
</ListView>

额外提示

  • 访问C:\Users\可能触发权限限制,建议先测试小目录(如C:\temp),避免因权限导致文件获取失败。
  • ObservableCollection已实现INotifyCollectionChanged,后续集合内容更新时会自动刷新UI。

内容的提问来源于stack exchange,提问作者dpatino

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 08:35:07