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;
            }
        }
    }
}
相关推荐
武藤一雄5 小时前
WPF处理耗时操作的7种方法
microsoft·c#·.net·wpf
Venom846 小时前
我的 WPF Powermill 工具
wpf
一念春风1 天前
证件照制作工具(WPF C#)
c#·wpf
He BianGu2 天前
【笔记】在WPF中GiveFeedbackEventHandler的功能和应用场景详细介绍
笔记·wpf
就是有点傻2 天前
WPF自定义控件-水晶球
wpf
He BianGu2 天前
【笔记】在WPF中QueryContinueDragEvent的详细介绍
笔记·wpf
He BianGu2 天前
【笔记】在WPF中QueryCursor事件的功能和应用场景详细介绍
笔记·wpf
He BianGu2 天前
【笔记】在WPF中CommandManager的功能和应用场景详细介绍
笔记·wpf
关关长语2 天前
HandyControl中Button图标展示多色路径
c#·.net·wpf·handycontrol
baivfhpwxf20233 天前
WPF DataGrid 指定列的数据可以编辑功能开发
wpf