Search code examples
c#wpflistboxwindow

how to update listbox from another window


I've got a AddWindow to add new client, the MainWindow (which is always showed) and I want to send the information from Addwindow to ListBox in MainWindow (I mean i need to add new item to listbox).

Someone knows how can I do that?


Solution

  • You can do that with events of that object like this :

     public partial class AddWindow : Window
    {
        public AddWindow()
        {
            InitializeComponent();
    
        }
    
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (Check != null)
                Check(TextBox.Text);
        }
    
    
        public event Action<string> Check;
    
    
    }
    

    and in main window

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();  
    
        }
    
    
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            AddWindow popup = new AddWindow();
            popup.Check += popup_Check;
            popup.Show();
    
        }
    
        void popup_Check(string obj)
        {
            ListBox.Items.Add(obj);
        }
    }