在另一个窗口上执行某些操作后更新 wpf 父窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18545961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Updating wpf parent window after some action on another window
提问by Ahmad Gulzar
i have one wpf window called usermanagement and there is a listbox showing all the users, i have one button in usermanagement window called add user and when i click on that new window opens called adduser, in this window there are input fields to add new user, what i need when i save data and this adduser window close then the usermanagement window update the listbox, means users again update (the new added user should show there after adding). at the moment i needed to open the usermanagement window again to see the new added user. Thanks!
我有一个名为 usermanagement 的 wpf 窗口,并且有一个显示所有用户的列表框,我在用户管理窗口中有一个名为 add user 的按钮,当我单击名为 adduser 的新窗口打开时,在此窗口中有用于添加新用户的输入字段,当我保存数据并且这个 adduser 窗口关闭然后用户管理窗口更新列表框时我需要什么,意味着用户再次更新(新添加的用户应该在添加后显示在那里)。目前我需要再次打开用户管理窗口以查看新添加的用户。谢谢!
here is the code below
这是下面的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;
using System.Collections;
using Model;
namespace Views
{
/// <summary>
/// Interaction logic for frmUserManagement.xaml
/// </summary>
public partial class frmUserManagement : Window
{
public frmUserManagement()
{
InitializeComponent();
}
public void window_loaded(object sender, RoutedEventArgs e)
{
load_users();
}
public void load_users()
{
RST_DBDataContext conn = new RST_DBDataContext();
var users = (from s in conn.TblUsers
select s.UserName).ToList();
Login_Names.ItemsSource = users;
}
private void add_user(object sender, RoutedEventArgs e)
{
adduser AddUserWindow = new adduser();
AddUserWindow.ShowDialog();
}
}
}
in xaml file there is
在 xaml 文件中有
<Grid>
<ListBox Name="Login_Names" HorizontalAlignment="Left" Height="337" Margin="10,47,0,0" Padding="0,0,0,0" VerticalAlignment="Top" Width="156">
<Button Content="Add" HorizontalAlignment="Left" Margin="10,404,0,0" VerticalAlignment="Top" Width="75" Click="add_user"/>
</Grid>
采纳答案by Jalal
Do insert operations inside main window (UserManagmentWindow):
在主窗口(UserManagmentWindow)内进行插入操作:
UserManagmentWindow.cs:
用户管理窗口.cs:
// Inside add button handler open adduser window as dialog box...
var result = adduser.ShowDialog();
if(result == true){
// user pressed OK button...
// insert new user in database
// refresh UserManagmentWindow
}
Post your code if you need more details...
如果您需要更多详细信息,请发布您的代码...
回答by Maple Wan
You can declare an event in Your AddUser window, and trigger the event when you press the button.
您可以在您的 AddUser 窗口中声明一个事件,并在您按下按钮时触发该事件。
First define your EventArgs child class
首先定义您的 EventArgs 子类
public class AddUserEventArgs : EventArgs
{
public User AddInfo { get; private set; }
public AddUserEventArgs(User info)
{
this.AddInfo = info;
}
}
In your AddUser class:
在您的 AddUser 类中:
public event EventHandler<AddUserEventArgs> AddedUser;
private void Button_Click(Object sender, RoutedEventArgs)
{
User info = new User();
// Realize your validation here...
// If validation is Okay, then..
if (OK)
{
if (this.AddedUser != null)
this.AddedUser(this, new AddUserEventArgs(info));
this.Close();
}
}
In your UserManagement class:
在您的 UserManagement 类中:
var window = new AddUserWindow();
window.AddedUser += (sender, e) =>
{
// Add the info to your ObservableCollection.
this.collection.Add(e.AddInfo);
}
window.ShowDialog();

