Using VS2019 - WPF App : My goal is to do a live-update of parent window ListBox by editing modeless child window TextBox, is it possible?
Imagine an app that has a list of users to which you can add, remove, edit and view. Add and Edit are working by one modal window with different methods. View has to be modeless window (so I made another class) and it has to do a live update of the user data in ListBox.
MainWindow.cs:
public UserDlg userDlg; //modal window (it has OK/cancel buttons)
public UserView userView; //modeless window (it has only Close button)
public List<User> users = new List<User>(); // a distinct user class that also has a method .getUser() that prints user data to String.
private void View(object sender, RoutedEventArgs e) { //view button
userView.Show();
userView.openEdition(users[sel].fName, users[sel].lName, users[sel].eMail);// it passes ListBox selected user data to new window (works)
}
private void UsersList_SelectionChanged(object sender, SelectionChangedEventArgs e){ //ListBox changes here
...if (userView != null && userView.IsVisible)
{
userView.openEdition(users[sel].fName, users[sel].lName, users[sel].eMail); // this updates selection in modeless window (works)
}
UserView.cs:
public void FName1_TextChanged(object sender, TextChangedEventArgs e)
{
Console.WriteLine(fName1.Text + " / " + lName1.Text + " / " + eMail1.Text); //this part is to show me that every character written/removed works fine
user.setUserData(getFirstName(), getLastName(), getMailAddress());
string dataToPass = user.getUser(); // - here I tried with RoutedEventHandler (something I found on web) but I managed to only send preinitialized string with a button - not a real-time text changed from textbox
}
public void openEdition(string fName, string lName, string eMail)
{
user = new User(fName, lName, eMail);
fName1.Text = fName;
lName1.Text = lName;
eMail1.Text = eMail;
}
I expect to change data in parent in realtime by changing textbox within modeless child.. any ideas?
EDIT: I used (some faulty) knowledge from Java I guess.. I thought of creating UserView with MainWindow window as param .. as a parent, it worked and I believe it somehow managed to solve my problem? - Testing it right now (will be back here in 10-15 minutes I hope.) >> UserView.cs:
MainWindow window;
public UserView(MainWindow window){ this.window = window; ... }
Now I can simply call window.usersList.Items.Add("Test"); for example.