C# 如何刷新WPF数据网格
我有一个绑定了C# 如何刷新WPF数据网格,c#,wpf,binding,datagrid,C#,Wpf,Binding,Datagrid,我有一个绑定了ItemsSource的DataGrid: <DataGrid x:Name="grid" FlowDirection="RightToLeft" AutoGenerateColumns="False" ItemsSource="{Binding ListGuards}" IsReadOnly="True"> 列定义(一个示例): 代码隐藏: public ObservableCollection<TableGua
ItemsSource的DataGrid
:
<DataGrid
x:Name="grid"
FlowDirection="RightToLeft"
AutoGenerateColumns="False"
ItemsSource="{Binding ListGuards}"
IsReadOnly="True">
列定义(一个示例):
代码隐藏:
public ObservableCollection<TableGuard> ListGuards { get; set; }
public C'tor(...) {
ListGuards = new ObservableCollection<TableGuard>(s); //s = List<TableGuard>
this.DataContext = this;
}
公共ObservableCollection ListGuards{get;set;}
公共行政部门(……){
ListGuards=新的ObservableCollection(s);//s=列表
this.DataContext=this;
}
提交数据:
private void submit(object sender, RoutedEventArgs e) {
if (list == null)
list = new List<TableGuard>();
TableGuard guard = new TableGuard();
guard.FName = fName.Text;
guard.LName = lName.Text;
guard.Id = int.Parse(id.Text);
guard.WorkPlace = workPlace.Text;
guard.LastTraining = lastTraining.Text;
guard.NextTraining = nextTraining.Text;
guard.Birthday = birthDay.Text;
guard.TrainingType = trainingType.Text;
bool exists = false;
foreach (var g in list)
if (guard.Id == g.Id) {
exists = true;
break;
}
if (exists)
MessageBox.Show("ID EXISTS!");
else if (id.Text.Length < 9)
MessageBox.Show("ID NEEDS TO BE 9 DIGITS!");
else {
if (isEdit) {
jsonObj[selectedRow]["Id"] = guard.Id;
jsonObj[selectedRow]["FName"] = guard.FName;
jsonObj[selectedRow]["LName"] = guard.LName;
jsonObj[selectedRow]["Birthday"] = guard.Birthday;
jsonObj[selectedRow]["LastTraining"] = guard.LastTraining;
jsonObj[selectedRow]["NextTraining"] = guard.NextTraining;
jsonObj[selectedRow]["WorkPlace"] = guard.WorkPlace;
jsonObj[selectedRow]["TrainingType"] = guard.TrainingType;
File.WriteAllText(path, JsonConvert.SerializeObject(jsonObj));
Close();
} else {
list.Add(guard);
File.WriteAllText(path, JsonConvert.SerializeObject(list));
Close();
}
}
}
private void submit(对象发送方、路由目标方){
if(list==null)
列表=新列表();
桌板防护装置=新的桌板防护装置();
guard.FName=FName.Text;
guard.LName=LName.Text;
guard.Id=int.Parse(Id.Text);
guard.WorkPlace=WorkPlace.Text;
guard.LastTraining=LastTraining.Text;
guard.NextTraining=NextTraining.Text;
guard.birth=birth.Text;
guard.TrainingType=TrainingType.Text;
bool exists=false;
foreach(列表中的变量g)
如果(guard.Id==g.Id){
存在=真;
打破
}
如果(存在)
显示(“ID存在!”);
否则如果(id.Text.Length<9)
Show(“ID必须是9位数字!”);
否则{
如果(isEdit){
jsonObj[selectedRow][“Id”]=guard.Id;
jsonObj[selectedRow][“FName”]=guard.FName;
jsonObj[selectedRow][“LName”]=guard.LName;
jsonObj[selectedRow][“生日”]=guard.birth;
jsonObj[selectedRow][“LastTraining”]=guard.LastTraining;
jsonObj[selectedRow][“NextTraining”]=guard.NextTraining;
jsonObj[selectedRow][“WorkPlace”]=guard.WorkPlace;
jsonObj[selectedRow][“TrainingType”]=guard.TrainingType;
writealText(路径,JsonConvert.SerializeObject(jsonObj));
Close();
}否则{
列表。添加(防护);
WriteAllText(路径,JsonConvert.SerializeObject(列表));
Close();
}
}
}
我希望在编辑一些数据后刷新
DataGrid
。如何操作?当您使用可观察集合时,当从列表中添加或删除项目时,您的网格将更新
但是,如果您正在实例化一个新的ObservableCollection
,那么您的网格将不会更新,因为您的属性没有实现INotifyPropertyChanged
在类中实现INotifyPropertyChanged
接口,并调用PropertyChanged
事件来更新网格:
private ObservableCollection<TableGuard> _ListGuards;
public ObservableCollection<TableGuard> ListGuards
{
get { return _ListGuards; }
set
{
_ListGuards = value;
OnPropertyChanged("ListGuards");
}
}
private observeCollection\u ListGuards;
公共可观测收集ListGuards
{
获取{return\u ListGuards;}
设置
{
_ListGuards=值;
OnPropertyChanged(“ListGuards”);
}
}
编辑:
在submit方法的末尾,只需重置ListGuards列表中的所有项目。调用.Clear()
方法并将项添加到集合中,或者在属性上实现INotifyPropertyChanged
后实例化一个新的ObservableCollection
。TableGuard类必须实现INotifyPropertyChanged并正确提升它。在数据网格的列定义中,必须将模式设置为双向。如果您在收藏中添加和删除项目,请查看其他答案
只是为了反对票:
如果调试代码,则在ctor中的行中
this.DataContext = this;
您的属性ListGuards属性为空,您从未使用ListGuards。添加/删除您不应在网格中看到任何内容。
在提交的方法中,您可以将ListGuard设置为list,也可以简单地添加ListGuard集合中的所有对象
grid.Items.Refresh();
我不知道这对你的情况是否有效
编辑:拼写您是否试图检测数据何时发生更改,即在事件中,然后刷新,或者只是在更改后如何刷新网格(您知道何时发生此情况)?当您编辑数据时,如果您有INotify,它将自动刷新。。在正确的位置向我们显示更多代码,并检查您的列definition@Shane.CI只是想刷新一下,我知道当数据改变时,正如我所说的,当你改变它时,它应该反映在屏幕上,向我们展示你的网格实现和改变数据,如果不是的话。一些代码来支持它将是非常棒的。好的,改变将作为一个可观察的colln通知!datacontext是在初始化之后设置的,因此不应该是problem@Muds正如我在回答中提到的,如果他正在实例化一个新的ObservableCollection
(ListGuards=new ObservableCollection;
),那么网格将不会刷新,除非属性实现INotifyPropertyChanged
,或者再次设置DataContext
。只需在属性中实现INotifyPropertyChanged
就容易多了。更不用说你不应该设置this.DataContext=this代码>无论如何。这恐怕是不对的,尽管我同意。dataContext=这不是最好的方法,但如果在设置数据上下文之前实例化集合,则应该可以…我希望在编辑一些数据后刷新DataGrid。我该怎么做@Muds您完全正确,构造函数中没有问题,但是OP在构造函数中没有问题,他在询问如何在更改一些数据后更新到ObservableCollection
的绑定。我假设他使用的是相同的方法,即实例化一个新的可观察集合
。我不是第一个投反对票的人,但如果你阅读下面的ans和评论,你就不会有这个问题,问题中的代码可能会回答正确。我在所有的项目中都这样做。苏轼治下的法典
grid.Items.Refresh();