从另一个窗口更新主窗口列表

时间:2019-07-15 10:01:02

标签: c# wpf wpf-listview

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

我具有CRUD功能来操作列表。 我打开一个新窗口以添加列表中的新项目。 在数据库中保存新项目之后。我正在从当前窗口调用主窗口的GetList函数。但问题是,列表未在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
    }

1 个答案:

答案 0 :(得分: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
    }
}