如何在WPF中检查DataGride中的所有复选框 [英] how to check all the checkbox in datagride in wpf
本文介绍了如何在WPF中检查DataGride中的所有复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
XMAIL代码:
<window x:class="WpfApplication5.MainWindow" xmlns:x="#unknown">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="550" Width="525">
<grid height="500" uselayoutrounding="False">
<Label Content="EID" Height="28" HorizontalAlignment="Left" Margin="136,90,0,0" Name="label1" VerticalAlignment="Top" Width="78" />
<textbox height="23" horizontalalignment="Left" margin="220,90,0,0" name="textBox1" verticalalignment="Top" width="120" />
<Label Content="Name" Height="28" HorizontalAlignment="Left" Margin="136,124,0,0" Name="label2" VerticalAlignment="Top" Width="78" />
<textbox height="23" horizontalalignment="Left" margin="220,124,0,0" name="textBox2" verticalalignment="Top" width="209" />
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="220,153,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
<datagrid autogeneratecolumns="False" height="200" margin="14,200,0,0" name="dataGrid1" verticalalignment="Top" width="477">
<datagrid.columns>
<datagridtemplatecolumn width="100">
<datagridtemplatecolumn.headertemplate>
<datatemplate>
<checkbox name="hcb" checked="hcb_Checked" horizontalalignment="Center"></checkbox>
</datatemplate>
</datagridtemplatecolumn.headertemplate>
<datagridtemplatecolumn.celltemplate>
<datatemplate>
<checkbox name="cb" horizontalalignment="Center"></checkbox>
</datatemplate>
</datagridtemplatecolumn.celltemplate>
</datagridtemplatecolumn>
<datagridtextcolumn header="EID" binding="{Binding ID}" width="200"></datagridtextcolumn>
<datagridtextcolumn header="NAME" binding="{Binding NAME}" width="250"></datagridtextcolumn>
</datagrid.columns>
</datagrid>
<grid.background>
<radialgradientbrush>
<gradientstop color="Chartreuse" offset="0" />
<gradientstop color="White" offset="1" />
</radialgradientbrush>
</grid.background>
</grid>
</window>
使用WPF的C#代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 WpfApplication5
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
#region
ServiceReference1.ServiceClient obj = new ServiceReference1.ServiceClient();
#endregion
public MainWindow()
{
InitializeComponent();
fill();
}
void fill()
{
dataGrid1.ItemsSource = obj.view();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
ServiceReference1.ValList x = new ServiceReference1.ValList();
x.ID = Convert.ToInt32(textBox1.Text);
x.NAME = textBox2.Text;
obj.insert(x);
MessageBox.Show("One record saved.");
fill();
}
private void hcb_Checked(object sender, RoutedEventArgs e)
{
MessageBox.Show("hi");
}
}
}
推荐答案
xaml代码:
<Window x:Class="WpfApplication5.Window3"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window3" Height="300" Width="300" Loaded="Window_Loaded" WindowStartupLocation="CenterScreen">
<Grid>
<DataGrid AutoGenerateColumns="False" Height="200" HorizontalAlignment="Left" Margin="33,20,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="200">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<CheckBox Name="hcb" Checked="hcb_Checked" Unchecked="hcb_Unchecked"></CheckBox>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox Name="cb" Width="50" IsChecked="{Binding Path = c , Mode = TwoWay}"></CheckBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Binding="{Binding s}" Header="NAME" Width="150"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
C#代码:
C# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Shapes;
namespace WpfApplication5
{
/// <summary>
/// Interaction logic for Window3.xaml
/// </summary>
public partial class Window3 : Window
{
public Window3()
{
InitializeComponent();
}
#region
ServiceReference1.ServiceClient o = new ServiceReference1.ServiceClient();
#endregion
private void Window_Loaded(object sender, RoutedEventArgs e)
{
List<xx> obj = new List<xx>();
for (int i = 0; i < o.view().ToList().Count;i++ )
{
xx y = new xx();
y.s = o.view().ToList()[i].NAME;
y.c = false;
obj.Add(y);
}
dataGrid1.ItemsSource = obj;
}
public class xx
{
public string s
{
get;
set;
}
public bool c
{
get;
set;
}
}
private void hcb_Checked(object sender, RoutedEventArgs e)
{
List<xx> obj = new List<xx>();
for (int i = 0; i < o.view().ToList().Count; i++)
{
xx y = new xx();
y.s = o.view().ToList()[i].NAME;
y.c = true;
obj.Add(y);
}
dataGrid1.ItemsSource = obj;
}
private void hcb_Unchecked(object sender, RoutedEventArgs e)
{
List<xx> obj = new List<xx>();
for (int i = 0; i < o.view().ToList().Count; i++)
{
xx y = new xx();
y.s = o.view().ToList()[i].NAME;
y.c = false;
obj.Add(y);
}
dataGrid1.ItemsSource = obj;
}
}
}
这篇关于如何在WPF中检查DataGride中的所有复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文