WPF子窗口关闭时更新父窗口数据
在父窗口中有一个listview进行数据库数据显示,和一个添加按钮,点击添加按钮后,弹出子窗口,在子窗口里输入数据信息后提交成功后返回主窗口,主窗口listview数据如何更新。
我使用了父窗口定义一个公用的refreshdata函数,在子窗口关闭时,调用这个refreshdata函数,结果无任何反应,但是该函数在父窗口执行完全没有问题。
请问在WPF里子窗口刷新父窗口数据有哪些解决方案呢?望达人指点,不胜感激!
[解决办法]
用Delegate来实现
自定义事件
[解决办法]
事件传值:
子窗体代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1 {
//定义委托
public delegate void ChangeTextHandler(string text);
public partial class ChildFrm : Form {
//定义事件
public event ChangeTextHandler ChangeTextEvent;
public ChildFrm() {
InitializeComponent();
}
/// <summary>
///
/// </summary>
/// <param name="sender">事件源</param>
/// <param name="e">事件对象</param>
private void radioButton1_CheckedChanged(object sender, EventArgs e) {
RadioButton rdo = sender as RadioButton;
//引发事件
if (ChangeTextEvent != null) {
ChangeTextEvent(rdo.Text);
}
}
private void ChildFrm_MouseClick(object sender, MouseEventArgs e) {
//e.Button == MouseButtons.Right
}
private void ChildFrm_Load(object sender, EventArgs e)
{
}
}
}
父窗体代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e) {
ChildFrm frm = new ChildFrm();
//订阅事件
frm.ChangeTextEvent += new ChangeTextHandler(frm_ChangeTextEvent);
frm.ShowDialog();
}
void frm_ChangeTextEvent(string text) {
this.textBox1.Text = text;
}
}
}
就这样