Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Developers,

I am in the situation where I want to refresh parent window Grid from child window button in WPF C#.

Let me explain.

I have a parent page where there is a list of records in Grid.
When I double click on any of the cell it opens one child window
where user can edit that row detail. After updating child window should be closed
and parent Grid should be refreshed.

Now problem is I am able to update the record in database from the child page
but I am not able to reflact that changes in parent window Grid.

I tried with raising buttom click event of parent page from child page but its not working.

Please guys any clue?
I need help to solve this problem.

Thanks in advance.
Posted
Updated 20-Nov-20 1:57am
Comments
format C: /U 19-Nov-15 4:37am    
What if both the parent and child are user controls, will this work on them as well?

1 solution

Not sure if I understand the situation correctly but you could define an event on the child window and subscribe that event on the parent window when yuo create the child.

Consider the following example:
Parent window XAML
XML
<Window x:Class="CP_CS_WPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:CP_CS_WPF"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" 
        Width="525">
    <Grid>
        <Button Content="Launch child" Click="Button_Click"/>
    </Grid>
</Window>

Parent window code
C#
using System;
using System.Windows;
namespace CP_CS_WPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Window1 window1 = new Window1();
            window1.DataChanged += Window1_DataChanged;
            window1.Show();
        }

        private void Window1_DataChanged(object sender, EventArgs e)
        {
            System.Windows.MessageBox.Show("Something has happened", "Parent");
        }
    }
}

Child window XAML
XML
<Window x:Class="CP_CS_WPF.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:CP_CS_WPF"
        mc:Ignorable="d"
        Title="Window1" 
        Height="300"
        Width="300">
    <Grid>
        <Button Content="Something happens" Click="Button_Click" />
    </Grid>
</Window>

Child window code
C#
using System;
using System.Windows;

namespace CP_CS_WPF
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public delegate void DataChangedEventHandler(object sender, EventArgs e);

        public event DataChangedEventHandler DataChanged;

        public Window1()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            DataChangedEventHandler handler = DataChanged;

            if (handler != null)
            {
                handler(this, new EventArgs());
            }
        }
    }
}
 
Share this answer
 
v3
Comments
kailash_tandel87 20-Sep-15 8:53am    
Thank you so much sir, You helped me to finish my job. Thanks a lot once again...:)
Wendelius 20-Sep-15 13:22pm    
Glad it helped :)
format C: /U 19-Nov-15 4:51am    
What if both the parent and child are user controls, will this work on them as well?
Member 9844487 1-Dec-16 5:40am    
How to do the same thing in MVVM pattern.
I create an action in child window. And close child window using this action, the same time I want to refresh my ParentWindow.
But here it's opening as new window as I created a new instance.
So how to refresh the existing window?

Is it good if I create a same eventhandelr in child view Model & on change I can subscribe to the parents View Model?


if (ChildVM.CloseAction == null)
{
ChildVM.CloseAction = new Action(() =>
{
loadwindow.Close();

mainWind = new MainWindow();
mainWind.DataContext = mvm;
mainWind.Show();
win.Close();
});
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900