WPF更新数据源

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 Leo.WpfTest
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.InitData();
}

internal void InitData()
{
IList<ModCls> modsCls = new List<ModCls>();
for (int i = 0; i < 60; i++)
{
ModCls mod = new ModCls();
mod.A = "A" + i;
mod.B = "B" + i;
mod.C = "C" + i;

modsCls.Add(mod);
}

this.gridCls.ItemsSource = modsCls;
this.gridCls.RefreshData();

}

private void Button_Click_1(object sender, RoutedEventArgs e)
{
ModCls mod = new ModCls();
mod.A = "A0";
mod.B = "BB";
mod.C = "CC";

IList<Leo.WpfTest.ModCls> temp = this.gridCls.ItemsSource as List<Leo.WpfTest.ModCls>;
IList<Leo.WpfTest.ModCls> tempA = (from p in temp
where p.A == mod.A
select p).ToList();
if (tempA != null && tempA.Count > 0)
{

for (int i = 0; i < temp.Count; i++)
{
if (temp[i].A == mod.A)
{
temp[i] = mod;
continue;
}
}

this.gridCls.RefreshData();
}
}
}

public class ModCls : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

private string a;
public string A
{
set
{
a = value;
if (PropertyChanged != null)//有改变
{
PropertyChanged(this, new PropertyChangedEventArgs("A"));
}
}
get
{
return a;
}
}
private string b;
public string B
{
set
{
b = value;
if (PropertyChanged != null)//有改变
{
PropertyChanged(this, new PropertyChangedEventArgs("B"));
}
}
get
{
return b;
}
}
private string c;
public string C
{
set
{
c = value;
if (PropertyChanged != null)//有改变
{
PropertyChanged(this, new PropertyChangedEventArgs("C"));
}
}
get
{
return c;
}
}
}
}

原文地址:https://www.cnblogs.com/binbinxiong/p/3721245.html