做不到這一點,如果你需要檢查的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