[英]Bind Pop-Up data into a screen from which Pop-up has triggered in WPF using 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.