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);
}
}文章来源:https://www.toymoban.com/news/detail-481184.html
}文章来源地址https://www.toymoban.com/news/detail-481184.html
到了这里,关于C#创建DataTable并填充数据,按钮事件实现全选,并到全选的值。wpf开发的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!