-1

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

我有 CRUD 功能来操作列表。我打开一个新窗口以在列表中添加新项目。在数据库中保存新项目后。我正在从当前窗口调用主窗口的 GetList 函数。但问题是,List 没有在 UI 上更新。我知道我正在新窗口中对主窗口进行新的引用,这可能是我的列表没有更新的原因。但我也不知道该怎么做。. 我获取列表的 .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();

    }

调用窗口代码-

 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
    }
4

1 回答 1

0

在从主窗口调用第二个窗口之前,将主窗口引用发送到第二个窗口,然后在这个引用中进行更改。样本:

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
    }
}
于 2019-07-15T10:18:53.217 回答