首页 > 代码库 > WPF DataGrid双向绑定,数据源数据更改 刷新列表(无需重新加载数据源~~~)
WPF DataGrid双向绑定,数据源数据更改 刷新列表(无需重新加载数据源~~~)
//假设变动的属性是“IsChanged”,
List<DataChangedViewModel> lstDataChanged = new List<DataChangedViewModel>();
//赋值省略
this.dataGrid.ItemsSource = lstDataChanged;
private void dataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
DataChangedViewModel mvm = dataGrid.SelectedItem as DataChangedViewModel;
if (mvm.IsChanged.Equals("False"))
{
foreach (DataChangedViewModel item in dataGrid.Items)
{
if (item.Id == mvm.Id)
{
item.IsChanged= "True";
}
}
}
}
public class DataChangedViewModel
: INotifyPropertyChanged
//实体类需要实现INotifyPropertyChanged 接口
{
public event PropertyChangedEventHandler PropertyChanged;
private string isChanged;
public int RowNum { get; set; }
public long Id { get; set; }
public string No { get; set; }
public string TypeName { get; set; }
public string StatusName { get; set; }
//变动的属性
public string IsChanged
{
get
{
return isChanged;
}
set
{
isChanged= value;
NotifyPropertyChanged("isChanged");
}
}
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
WPF DataGrid双向绑定,数据源数据更改 刷新列表(无需重新加载数据源~~~)