委托事件定义

 关闭子窗口告诉父窗体

父窗体
前段:
<Window x:Class="RefreshFatherWindow.WindowClose"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="WindowClose" Height="300" Width="300">
    <StackPanel>
        <TextBox Name="tb" />
        <Button Content="submit" Click="Button_Click" />
    </StackPanel>
</Window>

viewcode:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace RefreshFatherWindow
{
    /// <summary>
    /// Interaction logic for WindowClose.xaml
    /// </summary>
    public partial class WindowClose : Window
    {
        public WindowClose()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            SonWindow son = new SonWindow();

            son.Closed += new EventHandler(son_Closed);
            son.Show();
        }

        public void  son_Closed(object obj,EventArgs  e)
        {
            tb.Text = "儿子窗口已关闭!";
        }
    }
}

子窗体:
直接画个名为SonWindow.xaml的窗体,
点击关闭按钮X就会传值到父窗体


 

子窗体点击后传值到父窗体

父窗体
design:
<Window x:Class="RefreshFatherWindow.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <TextBox Name="tb" />
        <Button Content="submit" Click="Button_Click" />
    </StackPanel>
</Window>

viewCode:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace RefreshFatherWindow
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            SonWindow son = new SonWindow();
            son.ChangeTextEvent += new SonWindow.ChangeTextHandler(ChangeTextEvent);
            son.Show();
        }

        void ChangeTextEvent(string text)
        {
            tb.Text = text;
        }

    }
}

子窗体:
design
<Window x:Class="RefreshFatherWindow.SonWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="SonWindow" Height="300" Width="300">
    <Grid>
        <Button Content="点击窗口有效果" Click="Button_Click" />
    </Grid>
</Window>

viewcode:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace RefreshFatherWindow
{
    /// <summary>
    /// Interaction logic for SonWindow.xaml
    /// </summary>
    public partial class SonWindow : Window
    {
        //定义委托
        public delegate void ChangeTextHandler(string text);

        //定义事件
        public event ChangeTextHandler ChangeTextEvent;


        public SonWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
           ChangeTextEvent("我就是效果!");
        }
    }
}


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

来杯水

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值