简体   繁体   English

具有编辑记录按钮和多个编辑窗口的WPF MVVM Light DataGrid

[英]WPF MVVM Light DataGrid with edit record button and multiple edit windows

I am learning to use MVVM Light and I am making a program which uses database-first model with Entity Framework. 我正在学习使用MVVM Light,并且正在编写一个使用Entity Framework使用数据库优先模型的程序。

I have a DataGrid which is bound to a ViewModel, which gets data from a Repository.I am trying to make a button which when clicked opens a window, that is bound to its own View Model and lets you add a record to the DataGrid by adding it to the repository, that has an event, called when an item is added so it can update the DataGridViewModel. 我有一个绑定到ViewModel的DataGrid,该ViewModel从存储库获取数据。我试图制作一个按钮,单击该按钮会打开一个窗口,该窗口绑定到其自己的View Model,并允许您通过以下方式向DataGrid添加记录将其添加到具有事件的存储库中,该事件在添加项目时会调用,以便可以更新DataGridViewModel。

Everything here is fine. 这里的一切都很好。 I open the form, type in the data, press add and the record gets added to the database and the DataGrid gets refreshed automatically by the repository event. 我打开表单,输入数据,按添加,记录被添加到数据库中,并且存储库事件自动刷新DataGrid。

The problem comes when I open more than one "Add Record" windows. 当我打开多个“添加记录”窗口时,就会出现问题。 When I type something in one of the windows, it appears in the other one. 当我在其中一个窗口中键入内容时,它会出现在另一个窗口中。

I am aware that this is happening because both of the instances of the "Add Record" window are bound to the same instance of the View Model. 我知道这是因为“添加记录”窗口的两个实例都绑定到视图模型的同一实例而发生的。

Is there some way to resolve this problem so every window can be independent? 有什么方法可以解决此问题,以便每个窗口都可以独立吗? I know this does not look like much of a problem at the moment, but in the future I am planning to use that same window with the same view model to edit records (is that a good approach and can you recommend me some way of passing the record id or the record itself from the DataGrid window to the Add/Edit Record window?). 我知道目前这似乎不成问题,但将来我计划使用具有相同视图模型的同一窗口来编辑记录(这是一种好方法,您可以推荐我一些方法来进行传递吗?记录ID还是记录本身从DataGrid窗口到“添加/编辑记录”窗口?)。

Thanks in advance! 提前致谢! Tell me if I have missed something and I will add it:) 告诉我如果我错过了什么,我将添加它:)

Edit: Ok this is what I came up with: 编辑:好的,这就是我想出的:

public EditParentViewModel EditParent
    {
        get
        {
            EditParentViewModel editParentViewModelInstance = new EditParentViewModel(ServiceLocator.Current.GetInstance<IParentsRepository>(),
                ServiceLocator.Current.GetInstance<IChildrenRepository>(),
                ServiceLocator.Current.GetInstance<IBailiffsRepository>());
            return editParentViewModelInstance;
        }
    }

It is working but is this a correct way to supply the repositories to the View Model? 它正在工作,但这是将存储库提供给View模型的正确方法吗?

Counting that you are using ViewModelLocator , you should have something like this: 算上您正在使用ViewModelLocator ,您应该有类似以下内容:

public class ViewModelLocator
{
    public ViewModelLocator()
    {
        ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

        SimpleIoc.Default.Register<AddRecordViewModel>();
    }

    public AddRecordViewModel AddRecordViewModel
    {
        get { return ServiceLocator.Current.GetInstance<AddRecordViewModel>(); }
    }
}

Just return a new view model every time: 每次都返回一个新的视图模型:

public class ViewModelLocator
{
    public ViewModelLocator()
    {
    }

    public AddRecordViewModel AddRecordViewModel
    {
        get { return new AddRecordViewModel(); }
    }
}

That way your form always gets a new view model instead of getting the current instance allocated on your ServiceLocator . 这样,您的表单将始终获得新的视图模型,而不是在ServiceLocator上分配当前实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM