我的问题似乎很普遍,但是,对于我在这里和其他网站上阅读的所有帖子,我仍然没有找到解决方案。
我有一个相当简单的数据输入WPF模块-2个TextBoxes,3个ComboBoxes,1个DataGrid,然后单击Submit和Clear按钮。 (该应用程序/表单用于在会计数据库中创建总帐帐户。)我正在使用PRISM 5构建整个解决方案。(这是我首次涉足这种复杂的远程项目,目前,它是一个证明-无论如何,Iam将所有WPF屏幕(UserControl/Views)绑定(bind)到适当的ViewModels。然后,ViewModel通过EF 6实体(数据库优先)从MSSS数据库获取其数据。当用户打开此特定的WPF屏幕时,DataGrid将显示数据库中的所有现有记录。
除了一个异常(exception),Submit(新记录)过程按我的要求工作:将新条目插入MSSS db,清除文本框,并重置ComboBoxes。但是,我想要的是1)刷新DataGrid以显示新记录,以及2)在网格中突出显示该新记录。但是对于我的一生,我无法继续工作。注意:将DataGrid绑定(bind)到数据库中的 View 而不是表可能会有所不同。 (同样,当应用程序首次打开时,DataGrid可以正确显示。)
所以,我该如何更新DataGrid?
这是WPF View 的(精简)XAML:
<UserControl x:Class="AcctMappingWpfModule.Views.CreateGLAcctsView"
other namespace declarations...
xmlns:vms="clr-namespace:AcctMappingWpfModule.ViewModels">
<UserControl.DataContext>
<vms:CreateGLAcctsViewModel />
</UserControl.DataContext>
...
<StackPanel ...>
<!-- Layout controls for 2 Text boxes & 3 ComboBoxes -->
...
<!-- Data grid of all Genl Ledger accts -->
<DataGrid x:Name="dgGLAccts"
IsReadOnly="True"
SelectionUnit="FullRow"
...
ItemsSource="{Binding Path=GLAccounts}" />
<WrapPanel >
<!-- Submit & Clear Buttons here -->
</WrapPanel>
</StackPanel>
这是(压缩的)ViewModel代码(省略了try/catch块):
namespace AcctMappingWpfModule.ViewModels
{
公共(public)类CreateGLAcctsViewModel:BindableBase,ICreateGLAcctsViewModel
{
私有(private)TBLOADEntities上下文;
private int _glAcctID = 0;
//其他私有(private)领域...
// Ctor...
public CreateGLAcctsViewModel( )
{
this.SubmitCommand = new DelegateCommand(OnSubmit);
// Populate ICollectionViews - i.e., properties...
using (context = new TBLOADEntities())
{
// 3 properties behind ComboBoxes populated, then the DataGrid ppt...
List<vwGLAcct> accts = new List<vwGLAcct>();
accts = (from a in context.vwGLAcct select a).ToList<vwGLAcct>();
GLAccounts = CollectionViewSource.GetDefaultView(accts);
}
// Hook up selection change delegates, including...
GLAccounts.CurrentChanged += GLAccounts_CurrentChanged;
}
private void OnSubmit()
{
GLAccount glAcct = new GLAccount()
{
// Various properties set, then...
GlFsAcctTypeComboFK = this.SelectedFSAcctTypeComboID
};
using (context = new TBLOADEntities())
{
context.GlAcct.Add(glAcct);
context.SaveChanges();
// vwGLAcct is the EF entity of the MSSS db view...
List<vwGLAcct> accts = new List<vwGLAcct>();
accts = (from a in context.vwGLAcct select a).ToList<vwGLAcct>();
GLAccounts = CollectionViewSource.GetDefaultView(accts);
SelectedGLAcctID = glAcct.GlAcctID;
GLAccounts.Refresh();
}
}
private void GLAccounts_CurrentChanged(object sender, EventArgs e)
{
vwGLAcct current = GLAccounts.CurrentItem as vwGLAcct;
SelectedGLAcctID = current.GlAcctID;
}
public ICommand SubmitCommand { get; private set; }
public int SelectedGLAcctID
{
get
{ return _glAcctID; }
set
{
SetProperty(ref _glAcctID, value);
}
}
public ICollectionView GLAccounts { get; private set; }
}
}
最佳答案
1)为什么再次获得默认 View ? (CollectionViewSource.GetDefaultView(accts))提交后?确保accts是ObservableCollection并仅获取一次默认 View ,然后单击刷新。
2)是窗口后面的代码吗?如果是这样,则dgGLAccts.GetBindingExpression(DataGrid.ItemsSourceProperty).UpdateTarget()
关于c# - 无法使用PRISM 5,MVVM和EF 6刷新WPF中的DataGrid,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25750861/