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

C#实现分类列表联动子分类列表并点击打开新窗体方案咨询

双ListView实现分类-子分类联动(C# WinForms)

核心思路

用两个ListView分别承载父分类和子分类:

  • 父列表选中项变化时,根据选中的父分类从数据源提取对应子分类集合,填充到子列表
  • 子列表点击(或双击)时,触发新窗体打开逻辑

具体实现步骤

1. 定义数据结构

先定义分类实体类,方便数据管理:

// 父分类实体
public class MainCategory
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<SubCategory> SubCategories { get; set; } = new List<SubCategory>();
}

// 子分类实体
public class SubCategory
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string TargetFormName { get; set; } // 存储对应窗体的类名
}

2. 初始化ListView控件

在窗体Load事件中完成控件基础设置,同时加载父分类数据:

private List<MainCategory> _mainCategoryData;

private void Form1_Load(object sender, EventArgs e)
{
    // 父ListView配置
    listViewMain.View = View.Details;
    listViewMain.FullRowSelect = true;
    listViewMain.HideSelection = false;
    listViewMain.Columns.Add("分类名称", 200);

    // 子ListView配置
    listViewSub.View = View.Details;
    listViewSub.FullRowSelect = true;
    listViewSub.Columns.Add("子分类名称", 200);

    // 加载模拟数据(实际可从数据库/配置文件读取)
    LoadMainCategoryData();
}

private void LoadMainCategoryData()
{
    _mainCategoryData = new List<MainCategory>
    {
        new MainCategory
        {
            Id = 1,
            Name = "系统设置",
            SubCategories = new List<SubCategory>
            {
                new SubCategory { Id = 101, Name = "用户管理", TargetFormName = "UserManageForm" },
                new SubCategory { Id = 102, Name = "权限配置", TargetFormName = "PermissionForm" }
            }
        },
        new MainCategory
        {
            Id = 2,
            Name = "数据管理",
            SubCategories = new List<SubCategory>
            {
                new SubCategory { Id = 201, Name = "数据导出", TargetFormName = "DataExportForm" },
                new SubCategory { Id = 202, Name = "数据备份", TargetFormName = "DataBackupForm" }
            }
        }
    };

    // 填充父ListView
    foreach (var category in _mainCategoryData)
    {
        var item = new ListViewItem(category.Name);
        item.Tag = category; // 将分类实体绑定到Item的Tag,方便后续获取
        listViewMain.Items.Add(item);
    }
}

3. 父分类选中事件(填充子列表)

为父ListView添加SelectedIndexChanged事件,实现子列表动态更新:

private void listViewMain_SelectedIndexChanged(object sender, EventArgs e)
{
    // 清空子列表
    listViewSub.Items.Clear();

    if (listViewMain.SelectedItems.Count == 0) return;

    // 获取选中的父分类实体
    var selectedMainCat = listViewMain.SelectedItems[0].Tag as MainCategory;
    if (selectedMainCat == null) return;

    // 填充子分类到子ListView
    foreach (var subCat in selectedMainCat.SubCategories)
    {
        var item = new ListViewItem(subCat.Name);
        item.Tag = subCat; // 绑定子分类实体
        listViewSub.Items.Add(item);
    }
}

4. 子分类点击事件(打开新窗体)

为子ListView添加MouseDoubleClick事件,实现窗体跳转:

private void listViewSub_MouseDoubleClick(object sender, MouseEventArgs e)
{
    if (listViewSub.SelectedItems.Count == 0) return;

    var selectedSubCat = listViewSub.SelectedItems[0].Tag as SubCategory;
    if (selectedSubCat == null) return;

    // 通过反射实例化并打开对应窗体(也可直接写if-else判断)
    Type formType = Type.GetType($"YourProjectNamespace.{selectedSubCat.TargetFormName}");
    if (formType != null && typeof(Form).IsAssignableFrom(formType))
    {
        Form targetForm = (Form)Activator.CreateInstance(formType);
        targetForm.Show();
    }
}

优化与备选方案

  • 性能优化:若子分类数据量大,可提前缓存或异步加载,避免UI卡顿
  • 备选控件:如果层级结构更复杂,推荐使用TreeView控件,天然支持层级展示,无需双ListView联动,实现更简洁

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 03:15:15