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颜色(SolidColorBrush)和Win32颜色(COLOREF)互转的方法
wpf·win32·solidcolorbrush·colorref
shanshan20999 小时前
上位机系统架构 | 如何设计一个高效的多相机管理系统
c#·wpf·相机
充值内卷13 小时前
WPF入门教学四 WPF控件概述
windows·ui·wpf
小白1 天前
WPF自定义Dialog模板,内容用不同的Page填充
wpf
Crazy Struggle2 天前
C# + WPF 音频播放器 界面优雅,体验良好
c#·wpf·音频播放器·本地播放器
James.TCG2 天前
WPF动画
wpf
He BianGu2 天前
笔记:简要介绍WPF中FormattedText是什么,主要有什么功能
笔记·c#·wpf
脚步的影子3 天前
.NET 6.0 + WPF 使用 Prism 框架实现导航
.net·wpf
jyl_sh3 天前
Ribbon (WPF)
ribbon·wpf·client·桌面程序开发·c/s客户端
wo63704313 天前
[Visual Stuidio 2022使用技巧]2.配置及常用快捷键
c#·.net·wpf