代码
<ListView Height="303" HorizontalAlignment="Left" Name="listView1" VerticalAlignment="Top" Width="778" ItemsSource="{Binding Path=lists}">
<ListView.View>
<GridView>
<GridViewColumn x:Name="gvc">
<GridViewColumn.Header>
<CheckBox Click="CheckBox_Click_1">全选</CheckBox>
</GridViewColumn.Header>
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Click="CheckBox_Click" Tag="{Binding Path=Uid}"></CheckBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="用户id" DisplayMemberBinding="{Binding Path=Uid}" />
<GridViewColumn Header="用户名" DisplayMemberBinding="{Binding Path=Username}" />
<GridViewColumn Header="密 码" DisplayMemberBinding="{Binding Path=Password}" />
<GridViewColumn Header="操作">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Content="删除" Width="75" Height="23" Click="Button_Click" CommandParameter="{Binding Path=Uid}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
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 WPFGridView
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
private List<int> selectUid = new List<int>();//保存多选用户ID
private List<int> allUid = new List<int>();//保存全选用户ID
private List<User> list = new List<User>();//用户列表源数据
public MainWindow()
{
InitializeComponent();
this.DataBinding();
}
/// <summary>
/// 列表数据初始化并绑定
/// </summary>
private void DataBinding()
{
for (int i = 0; i < 5; i++)
{
User user = new User()
{
Uid = i + 1,
Username = "username" + i,
Password = "password"
};
list.Add(user);
}
this.listView1.ItemsSource = list;//为ListView绑定数据源
}
/// <summary>
/// 复选框删除用户
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, RoutedEventArgs e)
{
this.DeleteUsers(selectUid);
this.listView1.Items.Refresh();//刷新数据
}
/// <summary>
/// 由ChecBox的Click事件来记录被选中行的
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CheckBox_Click(object sender, RoutedEventArgs e)
{
CheckBox cb = sender as CheckBox;
int uid = Convert.ToInt32(cb.Tag.ToString()); //获取该行id
if (cb.IsChecked == true)
{
selectUid.Add(uid); //如果选中就保存id
}
else
{
selectUid.Remove(uid); //如果选中取消就删除里面的id
}
}
/// <summary>
/// 批量删除用户
/// </summary>
private void DeleteUsers(List<int> selectUid)
{
if (selectUid.Count > 0)
{
foreach (var uid in selectUid)
{
list.Remove(list[uid - 1]);
}
}
}
/// <summary>
/// 按钮单行删除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Click(object sender, RoutedEventArgs e)
{
Button b = sender as Button;
int uid = Convert.ToInt32(b.CommandParameter);
this.DeleteUser(uid);
this.listView1.Items.Refresh();
}
/// <summary>
/// 删除一个用户
/// </summary>
/// <param name="uid"></param>
private void DeleteUser(int uid)
{
list.Remove(list[uid - 1]);
}
/// <summary>
/// 全选
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CheckBox_Click_1(object sender, RoutedEventArgs e)
{
CheckBox cb = sender as CheckBox;
if (cb.IsChecked == true)
{
allUid = list.Select(l => l.Uid).ToList();
}
else
{
allUid.Clear();
}
}
}
}
评论 (0)