DataGrid on UserControl doesn't display binded ObservableCollection

前端 未结 1 1514
遥遥无期
遥遥无期 2020-12-22 07:46

I have a MainWindow with some buttons that loads a collection:



        
相关标签:
1条回答
  • 2020-12-22 08:14

    You have two instances of ResultViewModel, one in the MainWindowViewModel.ResultViewModel property, the other directly assigned to the UserControl's DataContext:

    <UserControl.DataContext>
        <local:ResultViewModel />
    </UserControl.DataContext>
    

    Remove the above from the UserControl's XAML.

    In general, a UserControl should never have its "own" view model, and you should never explicitly set its DataContext property in its XAML or code behind. Doing so overrides the value of the inherited DataContext, which contains the correct ResultViewModel instance.

    0 讨论(0)
提交回复
热议问题