C# WPF 从另一个窗口刷新列表框 [英] C# WPF refresh listbox from another window

查看:95
本文介绍了C# WPF 从另一个窗口刷新列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个基于 Access 数据库的 WPF 项目.数据库有两个表:

I'm making a WPF project based on an Access database. The database has two tables:

  • tblMovies(电影 ID:PK、标题、导演、类型等)
  • tblActors(ActorID:PK、MovieID:FK、名字、姓氏等)

我有一个列表框,我可以在其中查看所有电影,如果我点击其中一个,它会显示一个新窗口,其中包含该电影的所有详细信息:标题、导演、类型以及演员.

I have listbox where I can see all the movies, and if I click on one, it shows a new window with all the details about that movie: the title, director, genre, but also the actors.

在这个窗口中,我添加了一个按钮来创建一个新演员.这将打开一个新窗口,您可以在其中输入 MovieID (FK) 和有关演员的信息.当我单击保存更改时,它可以工作并且窗口关闭,但是我的 listboxActors 需要手动刷新(我为此添加了一个按钮)才能看到新的演员.

In this window I have added a button to create a new actor. This opens a new window where you can enter the MovieID (FK) and the information about the actor. When I click save changes, it works and the window closes, but my listboxActors needs to be manually refreshed (I have added a button for that) to see the new actor.

在我的另一个窗口中单击保存更改"后,有没有办法刷新我的 listboxActors?

Is there a way to refresh my listboxActors after I click "save changes" in my other window?

我首先在点击添加新演员时关闭我的第一个屏幕,然后如果我保存它会重新打开屏幕,它会自动刷新,但我不希望那样.

I first did it by closing my first screen when clicking add new actor, and then if I saved it would reopen the screen, and it'd automatically be refreshed, but I don't want it that way.

我的列表框演员:

        listBoxActors.ItemsSource = mov.Actors;

保存按钮(在另一个屏幕中)

Save button (in the other 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();
        }

刷新按钮:

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

提前致谢!

推荐答案

好吧,感谢您的解释性评论...!我有一个建议给你,如果这有助于你编写场景,请告诉我:

Well, Thanks for the explanatory comment...! I have a suggestion for you, please let me know if this helps you to code your scenario:

所以你有两个表单 WindowShowDetails 让它成为主表单,WindowAddActor 成为子表单,他们将是主表单中打开子表单的按钮表单,并且您正在子项中执行一些任务并按保存"按钮,这将保存这些详细信息并关闭该表单.并且您想刷新与此事件关联的主窗体中的列表.为此,您可以使用委托和事件;为此,您必须在主窗体和子窗体中执行许多任务,让我向您展示它如何帮助您:

So you have two forms WindowShowDetails Let it be the main-form and WindowAddActor be the child, the Their will be a button in the main form which opens the child form, and you are doing some tasks in the child and press the Save button, which will save those details and closes that form. And you wanted to refresh the List in the main form associated with this event. For this you can use delegates and events;For this you have to do a number of tasks, in the main-form as well as in the child, Let me show you how it can help you:

  • 在主窗体中定义一个委托:

  • Define a delegate in the main form:

public delegate void RefreshList();

  • 定义该委托类型的事件

  • Define an event of that delegate type

    public event RefreshList RefreshListEvent;
    

  • 定义一个方法来执行操作(即刷新​​网格)

  • Define a method that will do the action(ie, Refreshing the grid)

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

  • 现在需要在WindowAddActor

    公共代表更新Actor;

    Public Delegate UpdateActor;

    现在我们必须对打开 WindowAddActor 表单的按钮点击进行编码 让按钮为 btnAddActor 所以它的 Click 事件将是 btnAddActor_click,我们必须初始化我们的委托事件,即 WindowAddActor 的实例,并在显示该表单之前将事件分配给 WindowAddActor 中的 Delegate.这可以编码为:

    Now we have to code the button click that opens the WindowAddActor form Let the button be btnAddActor so its Click event will be btnAddActor_click, we have to initialize our delegate-event, the instance of the WindowAddActor and assign the event to Delegate in the WindowAddActor before showing that form. this can be coded as :

     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 的点击事件中调用委托

  • Now we have to call the delegate from the SaveButton's click event that is

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

  • 这篇关于C# WPF 从另一个窗口刷新列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆