繁体   English   中英

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

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

我正在使用MVVM模式开发WPF应用程序 ,其中的一个屏幕具有DataGrid,并且在单击的行中具有一个名为Add Unit的按钮,单击该按钮会打开一个弹出窗口,如下所示:

在此处输入图片说明 (我创建了一个新视图,并在单击此AddUnit按钮时调用了该视图)。因此,该弹出窗口再次具有一个包含多行的数据网格,如下所示: 在此处输入图片说明

我的问题是如何将弹出数据网格中的行数据(仅两列ItemCode和ItemName)绑定到主窗口(而不更改主窗口中DataGrid上方的数据),希望我有意义或是否存在其他任何正确的方法。

我真的很辛苦,因为我是WPF和MVVM的新手。任何帮助将不胜感激。

让我们使这些DataContexts(弹出窗口和从中打开弹出窗口的网格)共享同一服务(您可以在创建这些DataContext时将其注入到这些DataContext中)。 每次发生弹出行的新行网格选择时,都会更新此服务。 另外,它(该服务)将引发一个事件,以通知发生在Popup网格中的选择这一事实,并在EventArgs内部发送选择的数据。 从中打开弹出窗口的网格的数据上下文将侦听共享服务事件,并以您喜欢的方式更新其网格ItemSource集合。

更新资料

    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; }
}

共享服务代码

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);
    }
}

让我知道您是否需要更多信息。 问候。

暂无
暂无

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

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