本文介绍了WPF中的事件处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
窗口中有一个按钮 btn_Edit
。当该按钮点击时,将打开一个新窗口( new_win
),并将点击事件添加到按钮( btn_OK
)在 new_win
上。似乎 btn_OK_Click
不起作用,因为 new_win
不会关闭。问题在哪里?
BC_edit new_win = new BC_edit();
private void btn_Edit_Click(object sender,RoutedEventArgs e)
{
new_win.Title =a_title;
new_win.ShowDialog();
new_win.btn_OK.Click + = new RoutedEventHandler(btn_OK_Click);
}
private void btn_OK_Click(object sender,RoutedEventArgs e)
{
_MyCollection.Add(new MyData
{
Boundary = new_win .Title,
Type = new_win.cmb_BC_edit_type.SelectedItem.ToString(),
Option = new_win.cmb_BC_edit_options.SelectedItem.ToString()
});
new_win.Close();
}
解决方案
您需要订阅 );
new_win.ShowDialog();
请记住, ShowDialog
方法是阻止:它将不会返回,直到窗口关闭,所以以下语句将不会执行,直到那时。
There is a button btn_Edit
in a window. When that button clicked, a new window opens (new_win
) and a click event is added to a button (btn_OK
) on new_win
. It seems that btn_OK_Click
does not work because new_win
does not close. Where is the problem?
BC_edit new_win = new BC_edit();
private void btn_Edit_Click(object sender, RoutedEventArgs e)
{
new_win.Title = "a_title";
new_win.ShowDialog();
new_win.btn_OK.Click += new RoutedEventHandler(btn_OK_Click);
}
private void btn_OK_Click(object sender, RoutedEventArgs e)
{
_MyCollection.Add(new MyData
{
Boundary = new_win.Title,
Type = new_win.cmb_BC_edit_type.SelectedItem.ToString(),
Option = new_win.cmb_BC_edit_options.SelectedItem.ToString()
});
new_win.Close();
}
解决方案
You need to subscribe to the event before you show the new window:
new_win.btn_OK.Click += new RoutedEventHandler(btn_OK_Click);
new_win.ShowDialog();
Remember that the ShowDialog
method is blocking: it won't return until the window is closed, so the following statements won't be executed until then.
这篇关于WPF中的事件处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!