【问题标题】:Bind Pop-Up data into a screen from which Pop-up has triggered in WPF using MVVM使用 MVVM 将弹出窗口数据绑定到已在 WPF 中触发弹出窗口的屏幕
【发布时间】:2016-08-08 07:42:15
【问题描述】:

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

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

我的问题是我如何能够将弹出数据网格中的行数据(只有两列 ItemCode 和 ItemName)绑定到主窗口(不更改主窗口中 DataGrid 上方的数据)希望我有意义还是有其他正确的方法。

因为我是 WPF 和 MVVM 的新手,所以我真的很难接受。任何帮助都非常感谢。

【问题讨论】:

  • 你能发布你的主视图 ViewModel 的代码吗?具体来说,您是否有一个单独的 ViewModel 用于单行数据?您如何处理按钮点击?
  • 嗨,你能说得更具体点吗?这是什么意思 - “我怎样才能将弹出数据网格中的行数据(只有两列 ItemCode 和 ItemName)绑定到主窗口”?您想为弹出窗口提供网格行数据上下文吗?是否要为其提供网格行父数据上下文?
  • @Ilan 我在弹出窗口的 Datagrid 中有数据,我想将行数据(我可能点击任何行)绑定到弹出窗口打开的 Datagrid。
  • 所以我可以理解,你需要支持datagrid行数据到Popup的方式吗?你的 Popup 对象有自己的 DataContext 吗?
  • Yes Popup 有它自己的 DataContext,我想从弹出窗口的网格中获取行数据并添加到打开弹出窗口的网格中(我已经突出显示了图像中的按钮),你可以用任何简单的方法或程序帮助我。

标签: c# wpf xaml mvvm datagrid


【解决方案1】:

让我们让这些 DataContexts(弹出窗口和打开弹出窗口的网格)共享同一个服务(您可以在创建这些 DataContext 时将此服务注入这些 DataContext)。每次弹出窗口对新行的网格选择发生时,都会更新此服务。此外,它(服务)将引发一个事件来通知弹出网格中的选择发生的事实,并在 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);
    }
}

如果您需要更多信息,请告诉我。 问候。

【讨论】:

  • @IIan .,你能给我一些我一直在研究这个的代码示例...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-15
  • 2011-05-03
  • 2016-05-10
相关资源
最近更新 更多