C # WPF refresh list from another window

I am doing a WPF project based on an Access database. The database has two tables:

  • tblMovies (MovieID: PK, title, director, genre, etc.)
  • tblActors (ActorID: PK, MovieID: FK, first name, last name, etc.)

I have a list where I can see all the films, and if I click on it, a new window will appear with all the details about this film: title, director, genre, but also actors.

In this window, I added a button to create a new actor. This opens a new window in which you can enter MovieID (FK) and actor information. When I click save changes, it works and the window closes, but my listboxActors need to be updated manually (I added a button for this) to see the new member.

Is there a way to update my listboxActors after I click β€œsave changes” in another window?

At first I did this by closing my first screen when I click on a new actor, and then if I saved it, it will open the screen again and it will be automatically updated, but I do not want this.

My boxActors list:

        listBoxActors.ItemsSource = mov.Actors;

Save button (on another screen)

    private void buttonSaveNewActor_Click(object sender, RoutedEventArgs e)
        {
            Actor act = new Actor();
            act.MovieID = Convert.ToInt32(textBoxMovieID.Text);
            act.FirstName = textBoxFirstName.Text;
            act.LastName = textBoxLastName.Text;
            act.Country = textBoxCountry.Text;
            act.Born = Convert.ToDateTime(BornDate.SelectedDate);
            act.Bio = textBoxBio.Text;
            ActorRepository.AddActor(act);
            MessageBox.Show("The actor: " + act.FirstName + " " + act.LastName + " has been created");
            this.Hide();
        }

Refresh button:

    private void buttonRefresh_Click(object sender, RoutedEventArgs e)
    {
        listBoxActors.ItemsSource = null;
        listBoxActors.ItemsSource = mov.Actors;
    }

Thanks in advance!

+4
source share
1 answer

Ok, thanks for the explanatory comment ...! I have a suggestion for you, please let me know if this helps you copy your script:

, WindowShowDetails , WindowAddActor - , , , "", . , . , , , , , :

  • :

    public delegate void RefreshList();
    
  • public event RefreshList RefreshListEvent;
    
  • , (, )

    private void RefreshListView()
    {
        // Code to refresh the grid
    }
    
  • WindowAddActor

    Public Delegate UpdateActor;

  • , WindowAddActor. btnAddActor, Click btnAddActor_click, , WindowAddActor WindowAddActor, . :

     private void btnAddActor_click(object sender, EventArgs e)
     {
          WindowAddActor actorInstance = new WindowAddActor();      
          RefreshListEvent += new RefreshList(RefreshListView); // event initialization
          actorInstance.UpdateActor =  RefreshListEvent; // assigning event to the Delegate
          actorInstance.Show(); 
     }
    
  • SaveButton,

    private void buttonSaveNewActor_Click(object sender, RoutedEventArgs e)
    {
       // Perform save operation 
       UpdateActor.DynamicInvoke(); this will call the `RefreshListView` method of mainWindow
    
       this.Close();
    }
    
+2

Source: https://habr.com/ru/post/1665474/


All Articles