做不到这一点,如果你需要检查的DataContext是否被设置就可以使用DataContextChanged仅
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
DataContextChanged += new DependencyPropertyChangedEventHandler(UserControl1_DataContextChanged);
}
void UserControl1_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
// You can also validate the data going into the DataContext using the event args
}
}
注意,直到DataContext的从空转变为不同的值,它不会进入UserControl1_DataContextChanged。
不确定这是否回答您的问题,但在调试问题中使用起来可能非常方便。
不需要。我正在课堂上检查它。 – 2008-11-17 22:56:22
不知道自此答案以来是否有任何更改 - 但Loaded事件的上下文为NULL。 – Pakk 2016-04-09 03:20:21
在我的程序(.NET Framework 4.6.2)中,DataContext实际上是在InitializeComponent执行后初始化的。 – blearyeye 2017-12-20 20:39:11