我正在使用MVVM模式开发一个 WPF应用程序,其中一个屏幕有一个DataGrid,并且在点击时有一个名为Add Unit
的按钮,它会打开一个pop如下所示:
(我创建了一个新视图,并在单击此AddUnit按钮时调用此视图)。再次,此弹出窗口包含一个包含多行的数据网格,如下所示:
我的问题是如何能够将行数据(只有两列ItemCode和ItemName)从弹出数据网格绑定到主窗口(不更改主窗口中DataGrid上方的数据)希望我有意义或者还有其他正确的方法吗?
由于我是WPF和MVVM的新手,所以我真的很开心。任何帮助都非常感谢。
答案 0 :(得分:1)
让我们创建这些DataContexts(弹出窗口和打开弹出窗口的网格)共享相同的服务(您可以在创建它们时将这些服务注入这些DataContexts)。每次弹出新行的网格选择时,都会更新此服务。此外,它(服务)将引发一个事件,以通知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);
}
}
如果您需要更多信息,请与我们联系。 问候。