WPF ComboBox binding ItemsSource

前端 未结 3 1615
有刺的猬
有刺的猬 2020-12-16 04:17

I\'m a beginner on WPF and trying to bind the Items of a ComboBox to an ObservableCollection

I used this code:

XAML



        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-16 05:04

    cmbContent is null because you never set it to anything. I'm guessing the error is actually a NullReferenceException, but it is showing up as TargetInvocationException because it is in the constructor of a view.

    Also, you're setting the ItemsSource of the ComboBox twice (once in the binding, once in the constructor). You don't need to do that. Pick one. Your binding won't work the way it is written (because the DataContext isn't set) so you should either go with doing it in code, or set up the DataContext (as suggested by Nadia).

提交回复
热议问题