[英]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.