<Window x:Class="WPFTest.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">
<Window.Resources>
<Style TargetType="DataGridColumnHeader" >
<Setter Property="FontSize" Value="14" />
<Setter Property="Background" Value="#FFF4F4F4" />
<Setter Property="FontWeight" Value="Bold"/>
</Style>
<Style TargetType="DataGridRow">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="BorderBrush" Value="#FFF4F4F4" />
<Setter Property="BorderThickness" Value="1" />
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#FFF4F4F4"/>
</Trigger>
</Style.Triggers>
</Style>
<Style TargetType="DataGridCell">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="White"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Foreground" Value="White"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="0"/>
<RowDefinition Height="0"/>
<RowDefinition Height="*"/>
<RowDefinition Height="38"/>
</Grid.RowDefinitions>
<Border BorderBrush="#a4a4a4" Grid.Row="2" BorderThickness="1" Margin="10,5,10,5">
<DataGrid x:Name="DistributionTaskData" AlternatingRowBackground="#f1f8ff" SelectionMode="Single" HorizontalContentAlignment="Left" Margin="5,5,5,5" ColumnHeaderHeight="25" Grid.Column="0" CanUserAddRows="False" AutoGenerateColumns="False" Background="White" AlternationCount="0" SnapsToDevicePixels="False" BorderBrush="White" GridLinesVisibility="None" RowHeaderWidth="0" IsEnabled="True" DataContext="{Binding}" CanUserReorderColumns="False" CanUserSortColumns="False" ItemsSource="{Binding}" EnableRowVirtualization="False" MinRowHeight="30" Focusable="False" MinColumnWidth="100" BorderThickness="0" >
<DataGrid.Columns >
<DataGridTemplateColumn IsReadOnly="False">
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<CheckBox HorizontalAlignment="Center" VerticalAlignment="Center" Click="SelectedAll_Checked" Tag="{Binding}"
Name="ckApp" />
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox Name="cb" Tag="{Binding}" IsChecked="{Binding IsChecked,Mode=TwoWay}" Click="cb_Click" ></CheckBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="XXXXX" Binding="{Binding Name1}" ></DataGridTextColumn>
<DataGridTextColumn Header="YYYYY" Binding="{Binding Age}"></DataGridTextColumn>
<DataGridTextColumn Header="ZZZZZ" Binding="{Binding Addrress}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Border>
<Button Content="Button" Grid.Row="3" Height="23" HorizontalAlignment="Left" Margin="46,3,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
</Window>
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;
using System.ComponentModel;
namespace WPFTest
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
List<TestData> list = new List<TestData>();
public MainWindow()
{
InitializeComponent();
this.Loaded += MainWindow_Loaded;
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
GetData();
DistributionTaskData.ItemsSource = list;
}
private void SelectedAll_Checked(object sender, RoutedEventArgs e)
{
CheckBox chk1 = (CheckBox)sender;
List<TestData> vd1 = this.DistributionTaskData.ItemsSource as List<TestData>;
foreach (var item in vd1)
{
item.IsChecked = chk1.IsChecked.Value;
}
}
private void GetData()
{
for (int i = 0; i < 13; i++)
{
TestData td = new TestData();
td.Name1 = "text" + i;
td.Age = i + "";
td.Addrress = "dizhi" + i;
list.Add(td);
}
}
private void cb_Click(object sender, RoutedEventArgs e)
{
CheckBox chk = (CheckBox)sender;
TestData vd = chk.Tag as TestData;
vd.IsChecked = chk.IsChecked.Value;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
List<TestData> wellApproval = new List<TestData>();
if ((this.DistributionTaskData.ItemsSource as List<TestData>).Count > 0)
{
foreach (var item in this.DistributionTaskData.ItemsSource as List<TestData>)
{
if (item.IsChecked == true)
{
wellApproval.Add(item);
}
}
}
if (wellApproval.Count > 0)
{
StringBuilder str = new StringBuilder();
foreach (var item in wellApproval)
{
str.Append(item.Name1 + "||");
}
MessageBox.Show(str.ToString());
}
}
}
public class TestData : INotifyPropertyChanged
{
public void INotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
private string Name;
public string Name1
{
get { return Name; }
set { Name = value; }
}
private string age;
public string Age
{
get { return age; }
set { age = value; }
}
private string addrress;
public string Addrress
{
get { return addrress; }
set { addrress = value; }
}
private bool isChecked;
public bool IsChecked
{
get { return isChecked; }
set
{
isChecked = value;
INotifyPropertyChanged("IsChecked");
}
}
}
}