WPF 委托和事件实现子窗口回调函数, 实时刷新主窗口控件

注册窗口事件, 实时刷新主窗口控件

通常用于子窗口修改数据后实时刷新主窗口的ListView控件的场景


这个demo实现的功能是子窗口的TextBox中的数据实时显示到主窗口的Label


运行效果:



Solution Explore:



扫描二维码关注公众号,回复: 1572669 查看本文章

MainWindow.xaml

<Window x:Class="委托和事件实现子窗口回调函数.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:委托和事件实现子窗口回调函数"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="400">
    <Grid>
        <Button Name="buttonNewWindow" Content="Open SubWindow" Click="buttonNewWindow_Click" Margin="0,56,10,329"></Button>
        <Label Name="label" Margin="134,149,120,188"></Label>
    </Grid>
</Window>

MainWindow.xaml.cs

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 委托和事件实现子窗口回调函数
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void buttonNewWindow_Click(object sender, RoutedEventArgs e)
        {
            SubWindow subWindow = new SubWindow();
            subWindow.Activate();
            subWindow.Show();
            subWindow.updateMainwindowLabel += SubWindow_updateMainwindowLabel;
        }

        private void SubWindow_updateMainwindowLabel(string labelContent)
        {
            label.Content = labelContent;
        }
    }
}

SubWindow.xaml

<Window x:Class="委托和事件实现子窗口回调函数.SubWindow"
        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:委托和事件实现子窗口回调函数"
        mc:Ignorable="d"
        Title="SubWindow" Height="200" Width="300">
    <Grid>
        <Label Content="Content" Margin="26,56,193,82" RenderTransformOrigin="0.707,2.007"></Label>
        <TextBox Name="textBox" Margin="116,56,61,82" RenderTransformOrigin="0.487,1.402"></TextBox>
        <Button Name="buttonRefresh" Content="Refresh" Margin="0,136,0,0" Click="buttonRefresh_Click"></Button>
    </Grid>
</Window>

SunWindow.xaml.cs

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 委托和事件实现子窗口回调函数
{
    /// <summary>
    /// Interaction logic for SubWindow.xaml
    /// </summary>
    public partial class SubWindow : Window
    {
        public delegate void UpdateMainwindowLabel(string labelContent);
        public event UpdateMainwindowLabel updateMainwindowLabel;

        public SubWindow()
        {
            InitializeComponent();
        }

        private void buttonRefresh_Click(object sender, RoutedEventArgs e)
        {
            if (updateMainwindowLabel != null)
                updateMainwindowLabel(textBox.Text.Trim());
        }
    }
}



猜你喜欢

转载自blog.csdn.net/empty_android/article/details/79763501
今日推荐