Updating Main Window List from another window

366 Views Asked by At

I have a function in MainWindow.cs that gets a list from Database.

I have CRUD functionality to manipulate the list. I open a new window to ADD new item in list. After saving new item in DB. I am calling GetList function of Main Window from current window. But the Problem is, List is not updating on UI. I know i am making new refernce of Main Window in new window and may be that is the reason my list is not updating. But i also dont know how to do it. . My .CS code to get the List is:-

`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();

    }

Calling Window Code-

 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

There are 1 best solutions below

1
Serhat Oz On BEST ANSWER

Before calling second window from the main window sent main window referance to second window then do the changes in this referance. Sample:

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