前台绑定
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<DataGrid.Columns> <DataGridTemplateColumn > <DataGridTemplateColumn.HeaderTemplate> <DataTemplate> <CheckBox Click="HeaderCellLoad" Content="全选" IsChecked="False" Unchecked="UnChoice_Checked" Checked="Choice_Checked"></CheckBox> </DataTemplate> </DataGridTemplateColumn.HeaderTemplate> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <CheckBox Click="checkbox_checked" HorizontalAlignment="Center" x:Name="Choices" IsChecked="{Binding CHOICE, Mode=TwoWay}" VerticalAlignment="Center" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <DataGridTextColumn Header="商品条码" Binding="{Binding commodity_barcode}" Width="20*" HeaderStyle="{StaticResource ColumnHeaderStyle}"/> <DataGridTextColumn Header="商品名称" Binding="{Binding commodity_name}" Width="18*" HeaderStyle="{StaticResource ColumnHeaderStyle}"/> <DataGridTextColumn Header="单位" Binding="{Binding commodity_unit}" Width="10*" HeaderStyle="{StaticResource ColumnHeaderStyle}"/> <DataGridTextColumn Header="规格" Binding="{Binding commodity_rule}" Width="10*" HeaderStyle="{StaticResource ColumnHeaderStyle}"/> <DataGridTextColumn Header="售价" Binding="{Binding commodity_retailprice}" Width="10*" HeaderStyle="{StaticResource ColumnHeaderStyle}" /> <DataGridTextColumn Header="会员价" Binding="{Binding commodity_memberprice}" Width="10*" HeaderStyle="{StaticResource ColumnHeaderStyle}"/> <DataGridTemplateColumn Header="操作" Width="10*"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <Button Content="修改" Click="Update_Click" Margin="15,0,5,0" > <Button.Template> <ControlTemplate TargetType="Button"> <TextBlock TextDecorations="Underline" Name="btnInput" > <ContentPresenter /> </TextBlock> </ControlTemplate> </Button.Template> <Button.Style> <Style TargetType="Button"> <Setter Property="Foreground" Value="Blue" /> <Style.Triggers> <Trigger Property="IsMouseOver" Value="true"> <Setter Property="Foreground" Value="Red" /> </Trigger> </Style.Triggers> </Style> </Button.Style> </Button> <Button Content="删除" Click="Delete_Click" Margin="15,0,5,0"> <Button.Template> <ControlTemplate TargetType="Button"> <TextBlock TextDecorations="Underline" Name="btnCancel" > <ContentPresenter /> </TextBlock> </ControlTemplate> </Button.Template> <Button.Style> <Style TargetType="Button"> <Setter Property="Foreground" Value="Red" /> <Style.Triggers> <Trigger Property="IsMouseOver" Value="true"> <Setter Property="Foreground" Value="Red" /> </Trigger> </Style.Triggers> </Style> </Button.Style> </Button> </StackPanel> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns>
后台事件
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
/// <summary> /// Checked UnChecked事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Choice_Checked(object sender, RoutedEventArgs e) { DataTable dt = ((DataView)CommodityData.ItemsSource).Table; foreach (DataRow row in dt.Rows) { row["CHOICE"] = "1"; GetCount(); } } private void UnChoice_Checked(object sender, RoutedEventArgs e) { DataTable dt = ((DataView)CommodityData.ItemsSource).Table; foreach (DataRow row in dt.Rows) { row["CHOICE"] = "0"; GetCount(); } } private void checkbox_checked(object sender, RoutedEventArgs e) { //首先获取DataGridTemplateColumn所在列 DataGridTemplateColumn tempColumn = this.CommodityData.Columns[0] as DataGridTemplateColumn; //然后获取DataGridTemplateColumn单元格元素 FrameworkElement element = this.CommodityData.Columns[0].GetCellContent(this.CommodityData.Items[CommodityData.SelectedIndex]); if (element != null) { //把单元格元素转换为相应的控件,再从该控件中取值 System.Windows.Controls.CheckBox ck = tempColumn.CellTemplate.FindName("Choices", element) as System.Windows.Controls.CheckBox; if (ck.IsChecked == true) { DataRowView item = CommodityData.Items[CommodityData.SelectedIndex] as DataRowView; DataRow dr = item.Row; dr["CHOICE"] = "1"; GetCount(); } else if (ck.IsChecked == false) { DataRowView item = CommodityData.Items[CommodityData.SelectedIndex] as DataRowView; DataRow dr = item.Row; dr["CHOICE"] = "0"; GetCount(); } } }