简体   繁体   English

将弹出窗口数据绑定到使用MVVM在WPF中从中触发弹出窗口的屏幕

[英]Bind Pop-Up data into a screen from which Pop-up has triggered in WPF using MVVM

I am developing a WPF application using MVVM pattern in which one of the screen has a DataGrid and it has a button called Add Unit in a row on click of which it opens a pop-up as shown below: 我正在使用MVVM模式开发WPF应用程序 ,其中的一个屏幕具有DataGrid,并且在单击的行中具有一个名为Add Unit的按钮,单击该按钮会打开一个弹出窗口,如下所示:

在此处输入图片说明 (i created a new view and calling this view on click of this AddUnit button).So again this popup window has a datagrid with multiple rows as shown below: (我创建了一个新视图,并在单击此AddUnit按钮时调用了该视图)。因此,该弹出窗口再次具有一个包含多行的数据网格,如下所示: 在此处输入图片说明

My Question is how can I be able to bind the row data (only two columns ItemCode and ItemName)from Pop-up datagrid to the main window (without changing the data above the DataGrid in main Window) hope i am making sense or is there any other correct way of doing this. 我的问题是如何将弹出数据网格中的行数据(仅两列ItemCode和ItemName)绑定到主窗口(而不更改主窗口中DataGrid上方的数据),希望我有意义或是否存在其他任何正确的方法。

I really have a Hard-time with this as I am new to WPF and MVVM., any help greatly appreciate. 我真的很辛苦,因为我是WPF和MVVM的新手。任何帮助将不胜感激。

Let's make these DataContexts(popup's and the grid from which the popup is opened) share the same Service(you can inject this service to these DataContexts in time they are created). 让我们使这些DataContexts(弹出窗口和从中打开弹出窗口的网格)共享同一服务(您可以在创建这些DataContext时将其注入到这些DataContext中)。 This service will be updated each time the popup's grid selection of the new row is happens. 每次发生弹出行的新行网格选择时,都会更新此服务。 In addition it(the service) will raise an event to inform about the fact that the selection in Popup grid happened and sends the selected data inside the EventArgs. 另外,它(该服务)将引发一个事件,以通知发生在Popup网格中的选择这一事实,并在EventArgs内部发送选择的数据。 The data context of the grid from which the popup is opened will listen to the shared service event and will update its grid ItemSource collection in the way you like. 从中打开弹出窗口的网格的数据上下文将侦听共享服务事件,并以您喜欢的方式更新其网格ItemSource集合。

Update 更新资料

    public class MainGridDataContext:BaseObservableObject
    {
    private readonly ILikeEventAggregator _sharedService;
    //here we inject the the interface
    public MainGridDataContext(ILikeEventAggregator sharedService)
    {
        _sharedService = sharedService;
        //listen to selection changed
        _sharedService.PopupGridSelectionHandler += SharedServiceOnPopupGridSelectionHandler;
    }

    //uncomment next c'tor if you don't have any injection mechanism,
    //you should add the SharedService property to the App class
    //public MainGridDataContext()
    //{
    //    //_sharedService = App.Current.
    //    var app = Application.Current as App;
    //    if (app != null)
    //    {
    //        _sharedService = app.LikeEventAggregator;
    //        _sharedService.PopupGridSelectionHandler += SharedServiceOnPopupGridSelectionHandler;
    //    }
    //}

    private void SharedServiceOnPopupGridSelectionHandler(object sender, PopupGridData popupGridData)
    {
        UpdateGridWithAPopupSelectedData(popupGridData);
    }

    //method that helps to update the grid, you can do that in multiple ways
    private void UpdateGridWithAPopupSelectedData(PopupGridData popupGridData)
    {
        //Update your DataGrid here.
    }
}

public class PopupDataContext:BaseObservableObject
{
    private readonly ILikeEventAggregator _sharedService;
    //here we inject the the interface
    public PopupDataContext(ILikeEventAggregator sharedService)
    {
        _sharedService = sharedService;
    }

    //uncomment next c'tor if you don't have any injection mechanism,
    //you should add the SharedService property to the App class
    //public PopupDataContext()
    //{
    //    //_sharedService = App.Current.
    //    var app = Application.Current as App;
    //    if (app != null)
    //    {
    //        _sharedService = app.LikeEventAggregator;
    //    }
    //}

    //... your logic

    private PopupGridData _selectedData;
    //you should bind the popup grid selected value to this property
    public PopupGridData SelectedData
    {
        get { return _selectedData; }
        set
        {
            _selectedData = value;
            OnPropertyChanged(() => SelectedData);
            _sharedService.OnPopupGridSelectionHandler(_selectedData);
        }
    }

    //... your logic
}



public class PopupGridData
{
    public object PopupGridSelectedData { get; set; }
}

Shared service code 共享服务代码

public interface ILikeEventAggregator
{
    event EventHandler<PopupGridData> PopupGridSelectionHandler;
    void OnPopupGridSelectionHandler(PopupGridData e);
}

public class LikeEventAggregator : ILikeEventAggregator
{
    public event EventHandler<PopupGridData> PopupGridSelectionHandler;

    public virtual void OnPopupGridSelectionHandler(PopupGridData e)
    {
        var handler = PopupGridSelectionHandler;
        if (handler != null) handler(this, e);
    }
}

Let me know if you need more info. 让我知道您是否需要更多信息。 Regards. 问候。

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

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