WPF Getting Control null reference during InitializeComponent

前端 未结 6 2155
-上瘾入骨i
-上瘾入骨i 2021-02-05 10:13

So my InitializeComponent method call in the Window\'s constructor is running through the XML and adding the controls and plugging them into their events.

6条回答
  •  悲哀的现实
    2021-02-05 10:30

    Are any of the controls using two-way databinding? I've run into this issue where I've had textboxes bound to a properties on a ViewModel. The ViewModel initialization was triggering INotifyPropertyChanged up to the bound control, which in turn caused the textbox's TextChanged event to fire. My short-term workaround was to move the event subscription to the window Loaded event, but like you state, that's kind of a pain. I need to refactor the code to change the order in how my objects get initialized, so that the WPF Views (i.e. windows and user controls) are created before the ViewModels. Then I'll be able to move the event handler registration back into XAML.

提交回复
热议问题