在WPF C#中从子窗口刷新父窗口网格 [英] Refresh parent window Grid from child window in WPF C#

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

问题描述

您好开发者,



我想要在WPF C#中从子窗口按钮刷新父窗口网格。



让我解释一下。



我有一个父页面,其中有一个Grid格式的记录列表。

当我双击任何单元格时,它会打开一个子窗口

,用户可以在其中编辑该行详细信息。更新子窗口后应该关闭

和父网格应该刷新。



现在问题是我能够更新数据库中的记录从子页面

但是我无法在父窗口网格中反映出这些更改。



我尝试过提高buttom点击事件来自子页面的父页面,但它不起作用。



请大家有任何线索?

我需要帮助来解决这个问题。



提前致谢。

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.

推荐答案

不确定我是否正确了解情况但您可以在子窗口并在yuo创建子窗口时在父窗口上订阅该事件。



请考虑以下示例:

父窗口XAML

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
<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

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");
        }
    }
}



子窗口XAML


Child window XAML

<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

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());
            }
        }
    }
}


这篇关于在WPF C#中从子窗口刷新父窗口网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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