簡體   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