简体   繁体   中英

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:

在此处输入图片说明 (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: 在此处输入图片说明

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.

I really have a Hard-time with this as I am new to WPF and MVVM., any help greatly appreciate.

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). 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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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