The problem is that DataBinding is allowed when calling Show (and on InitializeComponent, but that is not important to you, because at that moment your DataContext is not set yet). I don't think you can prevent this, but I have an idea for a workaround:
Do not set the DataContext before calling Show (). You can achieve this (for example) as follows:
public partial class Window1 : Window { public Window1(object dataContext) { InitializeComponent(); this.Loaded += (sender, e) => { DataContext = dataContext; }; } }
and
protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); Window1 window = new Window1(new ViewModel()); window.Show(); }
Tim pohlmann
source share