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.
If you want create app on WPF with "best practice", then you will need read about MVVM on WPF.
But on this situation you need to know about (DataContext, ItemsSource), because on WPF data is in most cases moved by binding and ViewModels.
1 method. - use binding to common ViewModel.
UPDATE
If your ViewParent binding to field on ViewModel and ViewChild binding to ViewModel, than if you change data in ViewParent, than data will change in ViewModel, and data on ViewChild changed too(because it binding to ViewModel)
2 method
Bind to another View. Its bind like above, but you create binding to View and field on View.
Keywords: binding, databinding, mvvm, INPC.
Good links. I think it help to you MVVM: Tutorial from start to finish?
EDIT(UPDATE): Its wrong position on wpf. If you will not use binding, you convert wpf to winforms and will close for yourself 90% features of WPF.
You can use WinForms for this, its easily than wpf and have features like what you trying do(send link to parent view , etc)