日期:2014-05-18  浏览次数:20901 次

show()下的delegate就管用 为什么showdialog()下的delegate就失效了呢

今天无意中做了个小测试. 如果我写w.show();的话 delegate下的内容就可以命中
C# code

            Window1 w = new Window1();
            w.Show();
            w.Closed += delegate
            {
                dataGrid1.ItemsSource = IO.TE.Table.ToList();
                listBox1.ItemsSource = IO.TE.Table.ToList();
            };            

但是如果我写w.showdialog(); delegate可以命中 但是delegate的内容就不能命中了 这是为什么呢?

C# code
            Window1 w = new Window1();
            w.ShowDialog();
            w.Closed += delegate
            {
                dataGrid1.ItemsSource = IO.TE.Table.ToList();
                listBox1.ItemsSource = IO.TE.Table.ToList();
            };        


------解决方案--------------------
在ShowDialog之前绑定事件处理程序
也就是w.Closed += delegate在ShowDialog之前