Wpf DataGrid 数据绑定 排序 删除
初学wpf今天做一个菜鸟级别实例,只适合菜鸟.
先上图
说明一下功能:显示总条数,选中条数.全选.点击datagrid勾选,也可多选
datagrid绑定的数据是用的一个数据过渡类,如有一个Student类,类中有Id,Name...
但没有总条数也没有当前选中条数(当然还有其他情况会跟这种情况类似时也会用到)
所以我就构造一个数据过渡类StudentData
//数据实体类代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class PropertyChangedBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void Notify(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public class StudentData : PropertyChangedBase
{
private Student _student;
public Student student
{
get { return _student; }
set { _student = value; Notify("student"); }
}
private int _Count=0;
/// <summary>
/// 总条数
/// </summary>
public int Count
{
get { return _Count; }
set { _Count = value; Notify("Count"); }
}
private int _IsCheckedCount=0;
/// <summary>
/// 当前选中条数
/// </summary>
public int IsCheckedCount
{
get { return _IsCheckedCount; }
set { _IsCheckedCount = value; Notify("IsCheckedCount"); }
}
}
public class Student : PropertyChangedBase
{
private string _Id;
public string Id
{
get { return _Id; }
set { _Id = value; Notify("Id"); }
}
private string _Name;
public string Name
{
get { return _Name; }
set { _Name = value; Notify("Name"); }
}
public bool _IsChecked;
/// <summary>
/// 是否选中
/// </summary>
public bool IsChecked
{
get { return _IsChecked; }
set { _IsChecked = value; Notify("IsChecked"); }
}
}
数据实体已经写好,则需要绑定数据了,绑定数据代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/// <summary>
/// 数据过渡类集合
/// </summary>
List<StudentData> LStudentData = new List<StudentData>();
List<Student> stuList { get; set; }
public void GetData()
{
//获取学生数据集合
stuList = new List<Student> {
new Student{Id="001",Name="zenghai1",IsChecked=false},
new Student{Id="002",Name="zenghai2",IsChecked=false},
new Student{Id="003",Name="zenghai3",IsChecked=false},
new Student{Id="004",Name="zenghai4",IsChecked=false},
new Student{Id="005",Name="zenghai5",IsChecked=false},
};
//循环学生数据,并添加到数据过渡集合中
foreach (var item in stuList)
{
StudentData listStu = new StudentData();
listStu.student = item;
listStu.Count = stuList.Count;
listStu.IsCheckedCount = stuList.Count(p => p.IsChecked == true);
LStudentData.Add(listStu);
}
//绑定
Grid_Data.DataContext = LStudentData;
}
既然数据已经绑定好了,那就贴出xmal的代码,注意datagrid的 CanUserAddRows属性得置为false,不然DataGrid会
在尾部多出一行,意思为是否允许用户增加行.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<Grid Name="Grid_Data">
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<StackPanel Orientation="Horizontal">
<TextBlock VerticalAlignment="Center">
<TextBlock Text="当前总条数"></TextBlock>
<TextBlock Text="{Binding Path=Count}"></TextBlock>
<TextBlock Text="条"></TextBlock>
</TextBlock>
<TextBlock VerticalAlignment="Center" Margin="10,0,0,0" >
<TextBlock Text="当前选中条数"></TextBlock>
<TextBlock x:Name="Tb_SelectCount" Text="{Binding Path=IsCheckedCount}"></TextBlock>
<TextBlock Text="条"></TextBlock>
</TextBlock>
<Button Name="btn_delete" Click="btn_delete_Click" Content="删除选中" VerticalAlignment="Center" Margin="10,0,0,0"></Button>
</StackPanel>
</Grid>
<Grid Grid.Row="1">
<DataGrid Name="DgQuestion" ItemsSource="{Binding }" Margin="0" Background="White" SelectionChanged="DgQuestion_SelectionChanged" CanUserAddRows="False" AutoGenerateColumns="False" HorizontalGridLinesBrush="#FFD1CFCF" VerticalGridLinesBrush="#FFD1CFCF">
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="#DBEDF8"/>
<Setter Property="BorderBrush" Value="#DBEDF8"/>
<Setter Property="Foreground" Value="Black"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.Header>
<CheckBox Content="全 选" x:Name="cBox_All" Click="cBox_All_Click"></CheckBox>
</DataGridTemplateColumn.Header>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=student.IsChecked}"></CheckBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="名称" Width="*" Binding="{Binding Path=student.Name}" IsReadOnly="True"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Grid>
以下是页面中的事件处理
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
//全选按钮事件
private void cBox_All_Click(object sender, RoutedEventArgs e)
{
CheckBox cb = sender as CheckBox;
LStudentData.FindAll(p =>
{
p.student.IsChecked = cb.IsChecked.Value;
p.Count = stuList.Count;
return true; });
LStudentData.FindAll(p => { p.IsCheckedCount = stuList.Count(t => t.IsChecked == true); return true; });
}
//DataGrid SelectionChanged
private void DgQuestion_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (DgQuestion.SelectedItems.Count > 1)
{
foreach (var item in e.AddedItems)
{
if (item is StudentData)
{
bool isChecked = (item as StudentData).student.IsChecked;
(item as StudentData).student.IsChecked = true;
}
}
}
else
{
if (e.AddedItems.Count ==1)
{
if (e.AddedItems[0] is StudentData)
{
bool isChecked = (e.AddedItems[0] as StudentData).student.IsChecked;
if (isChecked)
(e.AddedItems[0] as StudentData).student.IsChecked = false;
else
(e.AddedItems[0] as StudentData).student.IsChecked = true;
}
}
}
LStudentData.FindAll(p => { p.IsCheckedCount = stuList.Count(t => t.IsChecked == true); return true; });
}
//删除选中按钮事件
private void btn_delete_Click(object sender, RoutedEventArgs e)
{
LStudentData = LStudentData.FindAll(p => { if (p.student.IsChecked) { p.student.IsChecked = false; return p.student.Id!=p.student.Id;} return true; });
LStudentData.FindAll(p => { p.Count = LStudentData.Count; return true; });
LStudentData.FindAll(p => { p.IsCheckedCount = LStudentData.Count(t => t.student.IsChecked == true); return true; });
if (LStudentData.Count == 0)
Tb_SelectCount.Text = "0";
Grid_Data.DataContext = LStudentData;
}
}
写博客是个很好的习惯,我会慢慢的坚持下去.如以上代码能更好的改进,或存在问题,请留言.