将新项目保存到数据库后,DataGrid仅显示此新项目。
例如,我的收藏夹中有
:尝试添加:
我所期望的:
我得到的是
我的VieModel继承自INotifyPropertyChanged
在我的ViewModel中,我加载数据:
using (var context = new DbContext())
{
var listOfbranches = context
.Branches_Table
.ToList();
Branches = new ObservableCollection<Branch>(listOfbranches);
}
我已经定义了ObservableCollection(可能是错误的定义):
private ObservableCollection<Branch> _Branches { get; set; }
public ObservableCollection<Branch> Branches
{
get { return _Branches; }
set
{
_Branches = value;
OnPropertyChanged("Branches");
}
}
然后按操作按钮:
public void MyAction()
{
var listOfbranches = new List<Branch>
{
new Branch { BranchCode = "1111", BranchID = "12121212"},
new Branch {BranchCode = "2222", BranchID = "21212121"}
};
using (var context = new DbContext())
{
context.Branches_Table.AddRange(listOfbranches );
context.SaveChanges();
Branches = new ObservableCollection<Branch>(listOfbranches);
}
}
在我的视图xaml中:
<DataGrid ItemsSource="{Binding Branches,Mode=TwoWay,NotifyOnSourceUpdated=True,UpdateSourceTrigger=PropertyChanged}" Grid.Row="1">
任何评论将不胜感激!