You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何通过代码修改ListView单元格内容?WPF动态列表实现咨询

解决方案:纯C#代码实现动态WPF ListView(带复选框交互)

Absolutely, you can build this dynamic ListView entirely in C# code without relying on MVVM bindings or data templates—let me walk you through a complete, working solution that covers column/row creation, data population, checkbox interactions, and updating cell content.

1. Core Data Structure (Track Row State)

First, we need a simple class to store each row's data and checkbox states. This keeps our data separate from UI elements, making it easier to update and sync later:

// Stores data for a single row to track text and checkbox states
private class RowData
{
    public string TextContent { get; set; }
    public Dictionary<string, bool> CheckBoxStates { get; set; }
}

2. Initialize the ListView & Dynamic Columns

Start by setting up the GridView (the view mode for your ListView) and adding columns dynamically. We'll use the Tag property on columns to identify them later:

private List<RowData> _allRowData = new List<RowData>();
private List<string> _checkColumnNames = new List<string>(); // Tracks checkbox column names

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    // 1. Define dynamic columns (1 text column + 3 checkbox columns as an example)
    _checkColumnNames = new List<string> { "列A", "列B", "列C" };
    
    // Create the GridView for the ListView
    var gridView = new GridView();
    
    // Add the text column
    var textColumn = new GridViewColumn
    {
        Header = "文本列",
        Width = 160
    };
    gridView.Columns.Add(textColumn);
    
    // Add checkbox columns
    foreach (var colName in _checkColumnNames)
    {
        var checkColumn = new GridViewColumn
        {
            Header = colName,
            Width = 110,
            Tag = colName // Store column name for easy identification later
        };
        gridView.Columns.Add(checkColumn);
    }
    
    // Assign the GridView to your empty ListView
    dynamicListView.View = gridView;
    
    // 2. Populate rows (we'll add this next)
    PopulateDynamicRows();
}

3. Populate Dynamic Rows with UI Elements

Now create rows with GridViewRow objects, add TextBlock for the first column, and CheckBox for the rest. We'll bind click events to handle interactions:

private void PopulateDynamicRows()
{
    // Generate 100 sample rows (replace with your actual data)
    for (int i = 0; i < 100; i++)
    {
        var rowData = new RowData
        {
            TextContent = $"行 {i + 1} 的文本内容",
            CheckBoxStates = _checkColumnNames.ToDictionary(name => name, _ => false) // Default to unchecked
        };
        _allRowData.Add(rowData);
        
        // Create the row UI element
        var row = new GridViewRow();
        
        // Add text cell
        var textCell = new GridViewCell();
        textCell.Content = new TextBlock { Text = rowData.TextContent };
        row.Cells.Add(textCell);
        
        // Add checkbox cells
        foreach (var colName in _checkColumnNames)
        {
            var checkCell = new GridViewCell();
            var checkBox = new CheckBox
            {
                IsChecked = rowData.CheckBoxStates[colName],
                HorizontalAlignment = HorizontalAlignment.Center
            };
            
            // Bind click event to handle column-wide updates
            checkBox.Click += (s, args) => OnCheckBoxClicked(s as CheckBox, colName, rowData);
            checkCell.Content = checkBox;
            row.Cells.Add(checkCell);
        }
        
        // Add the row to the ListView
        dynamicListView.Items.Add(row);
    }
}

4. Handle Checkbox Interaction Logic

Implement the click event to update the entire column's state (or any custom logic you need). We'll sync both the data model and UI elements:

private void OnCheckBoxClicked(CheckBox sender, string columnName, RowData currentRowData)
{
    bool newState = sender.IsChecked ?? false;
    
    // Update the current row's data
    currentRowData.CheckBoxStates[columnName] = newState;
    
    // Optional: Update ALL rows in this column (adjust logic to your needs)
    foreach (var rowData in _allRowData)
    {
        rowData.CheckBoxStates[columnName] = newState;
    }
    
    // Sync the UI to match the updated data
    foreach (GridViewRow row in dynamicListView.Items)
    {
        // Find the correct column index (text column is index 0, so checkbox columns start at 1)
        int columnIndex = _checkColumnNames.IndexOf(columnName) + 1;
        var checkCell = row.Cells[columnIndex];
        var checkBox = checkCell.Content as CheckBox;
        
        if (checkBox != null)
        {
            checkBox.IsChecked = newState;
        }
    }
}

5. Updating Specific Cell Content

To modify a cell's content later (like updating the text column or a checkbox), just target the corresponding UI element and sync the data model:

// Example: Update the text of the 5th row (index 4)
if (dynamicListView.Items.Count > 4 && dynamicListView.Items[4] is GridViewRow targetRow)
{
    var textCell = targetRow.Cells[0];
    if (textCell.Content is TextBlock textBlock)
    {
        textBlock.Text = "修改后的文本内容";
        // Sync the data model to avoid mismatches
        _allRowData[4].TextContent = "修改后的文本内容";
    }
}

// Example: Toggle the checkbox in "列B" for the 10th row (index 9)
if (dynamicListView.Items.Count > 9 && dynamicListView.Items[9] is GridViewRow targetRow)
{
    int columnIndex = _checkColumnNames.IndexOf("列B") + 1;
    var checkCell = targetRow.Cells[columnIndex];
    if (checkCell.Content is CheckBox checkBox)
    {
        checkBox.IsChecked = !checkBox.IsChecked;
        // Sync the data model
        _allRowData[9].CheckBoxStates["列B"] = checkBox.IsChecked ?? false;
    }
}

Key Notes

  • This approach avoids complex ViewModels and data templates, perfect for fully dynamic column/row scenarios.
  • Always sync UI changes with the RowData collection—this prevents data/UI mismatches as your app scales.
  • Adjust the checkbox interaction logic to fit your exact needs (e.g., only update related rows instead of the entire column).

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

火山引擎 最新活动