WPF datagrid 选中某一行后让第一列的checkbox选中

在 WPF 中的 DataGrid 中,如果希望在选中某一行后让该行的第一列中的 CheckBox 选中,可以通过绑定和事件处理来实现。以下是具体的步骤:

  1. 绑定数据 :确保 DataGrid 的数据源绑定到一个支持 INotifyPropertyChanged 接口的集合。
  2. 模板列定义 :定义一个带有 CheckBoxDataGridTemplateColumn,并绑定 CheckBoxIsChecked 属性。
  3. 事件处理 :处理 DataGridSelectionChanged 事件,在事件处理程序中设置 CheckBox 的选中状态。

以下是一个示例实现:

1. 数据模型

首先,定义一个数据模型,包含一个 IsChecked 属性,并实现 INotifyPropertyChanged 接口:

复制代码
using System.ComponentModel;

public class Item : INotifyPropertyChanged
{
    private bool _isChecked;
    public bool IsChecked
    {
        get { return _isChecked; }
        set
        {
            if (_isChecked != value)
            {
                _isChecked = value;
                OnPropertyChanged("IsChecked");
            }
        }
    }

    public string Name { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string name)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    }
}

2. XAML 定义

MainWindow.xaml 中,定义 DataGrid,绑定 ItemsSource 到一个集合,并定义一个包含 CheckBox 的模板列:

复制代码
<Window x:Class="WpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid x:Name="dataGrid" AutoGenerateColumns="False" SelectionChanged="DataGrid_SelectionChanged">
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Select">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

3. 代码隐藏

MainWindow.xaml.cs 中,设置 DataGrid 的数据源,并处理 SelectionChanged 事件:

复制代码
using System.Collections.ObjectModel;
using System.Windows;

namespace WpfApp
{
    public partial class MainWindow : Window
    {
        public ObservableCollection<Item> Items { get; set; }

        public MainWindow()
        {
            InitializeComponent();

            Items = new ObservableCollection<Item>
            {
                new Item { Name = "Item 1" },
                new Item { Name = "Item 2" },
                new Item { Name = "Item 3" }
            };

            dataGrid.ItemsSource = Items;
        }

        private void DataGrid_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (dataGrid.SelectedItem is Item selectedItem)
            {
                selectedItem.IsChecked = true;
            }
        }
    }
}
相关推荐
Scout-leaf1 天前
WPF新手村教程(三)—— 路由事件
c#·wpf
柒.梧.3 天前
基于SpringBoot+JWT 实现Token登录认证与登录人信息查询
wpf
十月南城6 天前
Flink实时计算心智模型——流、窗口、水位线、状态与Checkpoint的协作
大数据·flink·wpf
听麟9 天前
HarmonyOS 6.0+ 跨端会议助手APP开发实战:多设备接续与智能纪要全流程落地
分布式·深度学习·华为·区块链·wpf·harmonyos
@hdd9 天前
Kubernetes 可观测性:Prometheus 监控、日志采集与告警
云原生·kubernetes·wpf·prometheus
zls3653659 天前
C# WPF canvas中绘制缺陷分布map
开发语言·c#·wpf
专注VB编程开发20年9 天前
c#Redis扣款锁的设计,多用户,多台电脑操作
wpf
闲人编程10 天前
定时任务与周期性调度
分布式·python·wpf·调度·cron·定时人物·周期性
zls36536510 天前
C# WPF canvas中绘制缺陷分布map并实现缩放
开发语言·c#·wpf
数据知道11 天前
PostgreSQL:Citus 分布式拓展,水平分片,支持海量数据与高并发
分布式·postgresql·wpf