C#创建DataTable并填充数据,按钮事件实现全选,并到全选的值。wpf开发

wpf开发中,用事件创建一个datatable度填充到datagird里面,在datagrid里面有第一列是复选框。用一单击事件实现全选,用一个按钮事件得到所选中的值。

<Window x:Class="WpfApp4.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        xmlns:local="clr-namespace:WpfApp4"

        mc:Ignorable="d"

        Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded_1">

    <Grid>

        <DataGrid x:Name="DataGrid1">

            <DataGrid.Columns>

                <DataGridTemplateColumn>

                    <DataGridTemplateColumn.Header>

                        <CheckBox Click="SelectAll_Click">全选</CheckBox>

                    </DataGridTemplateColumn.Header>

                    <DataGridTemplateColumn.CellTemplate>

                        <DataTemplate>

                            <CheckBox Content="{Binding ID}" />

                        </DataTemplate>

                    </DataGridTemplateColumn.CellTemplate>

                </DataGridTemplateColumn>

            </DataGrid.Columns>

        </DataGrid>

        <Button x:Name="button" Content="批量删除测试得到所有id" HorizontalAlignment="Left" Margin="473,221,0,0" VerticalAlignment="Top" Click="Button_Click" />

    </Grid>

</Window>

using System;

using System.Collections.Generic;

using System.Data;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;

namespace WpfApp4

{

    /// <summary>

    /// MainWindow.xaml 的交互逻辑

    /// </summary>

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }

        private void Window_Loaded_1(object sender, RoutedEventArgs e)

        {

            // 创建 DataTable

            DataTable dataTable = new DataTable();

            // 添加列

            dataTable.Columns.Add("ID", typeof(int));

            dataTable.Columns.Add("Name", typeof(string));

            dataTable.Columns.Add("Age", typeof(int));

            // 添加行

            for (int i = 1; i <= 10; i++)

            {

                DataRow row = dataTable.NewRow();

                row["ID"] = i;

                row["Name"] = "Name " + i;

                row["Age"] = 20 + i;

                dataTable.Rows.Add(row);

            }

            // 将 DataTable 绑定到 DataGrid1

            DataGrid1.ItemsSource = dataTable.DefaultView;

        }

       

       //实现全选

        private void SelectAll_Click(object sender, RoutedEventArgs e)

        {

            CheckBox headerCheckBox = (CheckBox)sender;

            bool isChecked = headerCheckBox.IsChecked ?? false;

            //MessageBox.Show(isChecked.ToString());

            //if(isChecked==true)

            foreach (var item in DataGrid1.Items)

            {

                if (item is DataRowView yourItem)

                {

                    // Find the CheckBox control in the first column

                    DataGridCell cell = DataGrid1.Columns[0].GetCellContent(yourItem).Parent as DataGridCell;

                    CheckBox checkBox = FindVisualChild<CheckBox>(cell);

                    if (checkBox != null)

                    {

                        //checkBox.IsChecked = true;

                        checkBox.IsChecked = isChecked;

                    }

                }

            }

        }

        // Helper method to find a child control of a certain type in a visual tree

        /// <summary>

        /// 查找第一列所有子元素。

        /// </summary>

        /// <typeparam name="T"></typeparam>

        /// <param name="parent"></param>

        /// <returns></returns>

        private T FindVisualChild<T>(DependencyObject parent) where T : DependencyObject

        {

            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)

            {

                DependencyObject child = VisualTreeHelper.GetChild(parent, i);

                if (child != null && child is T)

                {

                    return (T)child;

                }

                else

                {

                    T childOfChild = FindVisualChild<T>(child);

                    if (childOfChild != null)

                    {

                        return childOfChild;

                    }

                }

            }

            return null;

        }

        //得到所有id,用于批量删除。

        private void Button_Click(object sender, RoutedEventArgs e)

        {

            string ids = "";

            foreach (var item in DataGrid1.Items)

            {

                if (item is DataRowView yourItem)

                {

                    // Find the CheckBox control in the first column

                    DataGridCell cell = DataGrid1.Columns[0].GetCellContent(yourItem).Parent as DataGridCell;

                    CheckBox checkBox = FindVisualChild<CheckBox>(cell);

                    if (checkBox != null)

                    {

                        ids = ids + checkBox.Content + ",";

                    }

                }

            }

            MessageBox.Show(ids);

        }

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值