1、ListView要实现实时更新必须要有几个条件:
(1)需要将List集合定义为ObservableCollection类型,改类型需要引用System.Collections.ObjectModel命名空间。
(2)实体类需要继承INotifyPropertyChanged接口
MainVindow.xaml.cs:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 WpfApp3
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
private static ObservableCollection<UserData> List = new ObservableCollection<UserData>();
private int SelectedIndex = -1;
public MainWindow()
{
InitializeComponent();
UserList.ItemsSource = List;
}
private void UserList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
SelectedIndex = Convert.ToInt32(UserList.SelectedIndex.ToString());
}
private void Add_Click(object sender, RoutedEventArgs e)
{
int count = List.Count;
int i = count + 1;
List.Add(new UserData() { id = i, uin ="姓名"+i, pass="密码"+i });
}
private void Del_Click(object sender, RoutedEventArgs e)
{
if (SelectedIndex >= 0)
{
List.RemoveAt(SelectedIndex);
}
else
{
MessageBox.Show("没有选中");
}
}
private void Edit_Click(object sender, RoutedEventArgs e)
{
if (SelectedIndex >= 0)
{
List[SelectedIndex].id = 88;
List[SelectedIndex].uin = "张三";
List[SelectedIndex].pass = "123456";
}
else
{
MessageBox.Show("没有选中");
}
}
}
}
MainWindow.xaml
<Window x:Class="WpfApp3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp3"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="320"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<ListView x:Name="UserList" HorizontalAlignment="Left" Height="300" Margin="10" VerticalAlignment="Top" Grid.Row="0" SelectionChanged="UserList_SelectionChanged">
<ListView.View>
<GridView>
<GridViewColumn Header="序号" Width="50" DisplayMemberBinding="{Binding id}"/>
<GridViewColumn Header="账号" Width="200" DisplayMemberBinding="{Binding uin}"/>
<GridViewColumn Header="密码" Width="200" DisplayMemberBinding="{Binding pass}"/>
</GridView>
</ListView.View>
</ListView>
<Grid Grid.Row="1" Background="AliceBlue">
<StackPanel Orientation="Horizontal">
<Button Content="添加" Width="100" Height="30" Click="Add_Click" Margin="10"></Button>
<Button Content="删除" Width="100" Height="30" Click="Del_Click" Margin="10"></Button>
<Button Content="修改" Width="100" Height="30" Click="Edit_Click" Margin="10"></Button>
</StackPanel>
</Grid>
</Grid>
</Window>
UserData.cs(实体类)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace WpfApp3
{
//UserData的定义:
class UserData : INotifyPropertyChanged //通知接口
{
public event PropertyChangedEventHandler PropertyChanged;
private string _uin;
private string _pass;
private int _id;
public string uin
{
get { return _uin; }
set
{
_uin = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("uin"));
}
}
public string pass
{
get { return _pass; }
set
{
_pass = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("pass"));
}
}
public int id
{
get { return _id; }
set
{
_id = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("id"));
}
}
public UserData()
{
_id = id;
_uin = uin;
_pass = pass;
}
}
}