从另一个 window 更新主要 Window 列表

Updating Main Window List from another window

我在 MainWindow.cs 中有一个函数可以从数据库中获取列表。

我有 CRUD 功能来操作列表。 我打开一个新的 window 以在列表中添加新项目。 在数据库中保存新项目后。我正在从当前 window 调用 Main Window 的 GetList 函数。但问题是,List 没有在 UI 上更新。 我知道我在新的 window 中对 Main Window 进行了新的引用,这可能是我的列表没有更新的原因。但我也不知道该怎么做。 .我获取列表的 .CS 代码是:-

`public List<VaultRecordLine> GetVaultRecordLines()
        {
            weekTaskView = new WeekTaskViewModel();
            var result = weekTaskView.getVaultRecordLines();
            List<VaultRecordLine> list = new List<VaultRecordLine>();
            foreach (var item in result.Entities)
            {
                VaultRecordLine vrl = new VaultRecordLine();

                if (item.Attributes.Contains("createdby"))
                {
                    vrl.CreatedBy = item.Attributes["createdby"].ToString();
                }
                if (item.Attributes.Contains("new_account"))
                {
                    vrl.Host = item.Attributes["new_account"].ToString();
                }
                if (item.Attributes.Contains("new_login"))
                {
                    vrl.Login = item.Attributes["new_login"].ToString();
                }
                if (item.Attributes.Contains("new_password"))
                {
                    vrl.Password = item.Attributes["new_password"].ToString();
                }
                if (item.Attributes.Contains("new_vaultid"))
                {
                    vrl.Id = new Guid(item.Attributes["new_vaultid"].ToString());
                }

                list.Add(vrl);
                gdDecryptVault.ItemsSource = list;
                gdDecryptVault.Items.Refresh();// This line refreshes the List
            }
            return list;
        }

`

 private void btnOpenModal_Click(object sender, RoutedEventArgs e)
    {
        AddNewVaultLineModalWindow modalWindow = new AddNewVaultLineModalWindow();
        modalWindow.ShowDialog();

    }

调用Window代码-

 private void saveNewVaultLine_Click(object sender, RoutedEventArgs e)
    {
        WeekTaskViewModel weekTaskView;
        MainWindow mw;
        weekTaskView = new WeekTaskViewModel();
        mw = new MainWindow();
        VaultRecordLine vaultRecordLine = new VaultRecordLine();
        vaultRecordLine.Host = Host.Text;
        vaultRecordLine.Login = Login.Text;
        vaultRecordLine.Password = Password.Text;
        vaultRecordLine.IsPasswordVisible = (bool)PrivatePassword.IsChecked;
        weekTaskView.SaveNewVaultLine(vaultRecordLine);
        mw.GetVaultRecordLines();// This Lines call the Main Window function to get records to list
    }

在从主 window 调用第二个 window 之前,将主 window 引用发送给第二个 window 然后在此引用中进行更改。示例:

class MainWindow
{

    public void callToSecondWindow(){
        SecondForm sf = new  SecondForm();
        sf.firstWindowRef = this;
        sf.showDialog();
    }
}

class SecondWindow
{

    MainWindow mvRef;

    private void saveNewVaultLine_Click(object sender, RoutedEventArgs e)
    {
        WeekTaskViewModel weekTaskView;
        weekTaskView = new WeekTaskViewModel();
        VaultRecordLine vaultRecordLine = new VaultRecordLine();
        vaultRecordLine.Host = Host.Text;
        vaultRecordLine.Login = Login.Text;
        vaultRecordLine.Password = Password.Text;
        vaultRecordLine.IsPasswordVisible = (bool)PrivatePassword.IsChecked;
        weekTaskView.SaveNewVaultLine(vaultRecordLine);
        mvRef.GetVaultRecordLines();// This Lines call the Main Window function to get records to list
    }
}