How to update list on one window when some event trigger on another window in WPF

后端 未结 3 1346
忘了有多久
忘了有多久 2021-01-25 15:40

How to update list on one window when some event trigger on another window in WPF. i just want to know how to listen to the event of one window from another window.

相关标签:
3条回答
  • 2021-01-25 15:58

    What did you tried so far?

    Is one window creating the other window?

    Normally i would call a method of the child window. If the child window wants to trigger the parent, it should be done with an event.

    like: pseudo

    public class FormParent : Form
    {
    
        public void OpenChild()
        {
            // create the child form
            FormChild child = new FormChild();
    
            // register the event
            child.DataUpdated += Child_DataUpdated;
    
            // ....
    
            // parent to child (method call)
            child.DoSomething();
        }
    
        public void Child_DataUpdated(object sender, EventArgs e)
        {
             // refresh the list.
        }
    
    }
    
    public class FormChild : Form
    {
    
        public void DoSomething()
        {
    
            // ....
    
            // if the list should be refreshed.
    
            // call event from child to parent.
            if(DataUpdated != null)
                DataUpdated(this, EventArgs.Empty);
        }
    
    
        public event EventHandler DataUpdated;
    }
    

    Keep in mind, the parent knows the structure of the child. (method call) The child, doesn't know anything about the structure of the parent (events)

    This way you can reuse the child on different solutions. (not dependend on code)

    0 讨论(0)
  • 2021-01-25 16:11

    This is basically same question like passing data between two windows. Sender is the window which has trigger, receiver is the one what process.

    You can have coupled solution, when one of window should know about other. If you pass instance of receiver to sender, then sender will be able to call a public method of receiver. If you pass instance of sender to receiver, then receiver can subscribe to event of public control (don't do that) or special dedicated form event (just for this reason - to trigger something somewhere else).

    Decoupled solution would be to have separate class (provider?), which exposes event and method to trigger that event. Receiver will subscribe to event, sender will trigger it, none of them knows about each other, none must trigger or subscribe.

    0 讨论(0)
  • 2021-01-25 16:23

    You'll have to pass the object to the new window and then create a new event handler for it on the second window.

    enter image description here

    First Window Code:

    public FirstWindow()
    {
        InitializeComponent();
    }
    
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        SecondWindow sWindow = new SecondWindow(btnFirstWindow);
        sWindow.Show();
    }
    

    Second Window Code:

    private Button firstWindowButton;
    public SecondWindow(Button firstWindowButton)
    {
        this.firstWindowButton = firstWindowButton;
    
        InitializeComponent();
    }
    
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        firstWindowButton.Click += firstWindowButton_Click;
    }
    
    void firstWindowButton_Click(object sender, RoutedEventArgs e)
    {
        lblShowUser.Content = "First window button clicked on: " + DateTime.Now.ToString();
    }
    

    I've added a list to the first window and events on the second window for you:

    enter image description here

    Here is the first windows code:

    public FirstWindow()
    {
        InitializeComponent();
    }
    
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        String[] items = { "Item 1", "Item 2", "Item 3" };
        listItems.ItemsSource = items;
    
        SecondWindow sWindow = new SecondWindow(btnFirstWindow, listItems);
        sWindow.Show();
    }
    

    Second windows code:

    private Button firstWindowButton;
    private ListBox firstWindowListBox;
    
    public SecondWindow(Button firstWindowButton, ListBox firstWindowListBox)
    {
        this.firstWindowButton = firstWindowButton;
        this.firstWindowListBox = firstWindowListBox;
        InitializeComponent();
    }
    
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        firstWindowButton.Click += firstWindowButton_Click;
        firstWindowListBox.MouseDoubleClick += firstWindowListBox_MouseDoubleClick;
    }
    
    void firstWindowListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        if (firstWindowListBox.SelectedItem != null)
        {
            lblShowUser.Content = "First window list box selected item: " + firstWindowListBox.SelectedItem.ToString();
        }
    }
    
    void firstWindowButton_Click(object sender, RoutedEventArgs e)
    {
        lblShowUser.Content = "First window button clicked on: " + DateTime.Now.ToString();
    }
    
    0 讨论(0)
提交回复
热议问题