C# WPF ListView.ItemsSource绑定问题

C# WPF ListView.ItemsSource绑定问题,c#,.net,mvvm,binding,C#,.net,Mvvm,Binding,我有一个MainViewModel和Customers属性(CustomerServiceModel对象列表作为ObservableCollection)。我想将该列表作为ItemsSource绑定到ListView控件。我的MainViewModel绑定到窗口的DataContext属性。我使用ServiceLocator模式创建MainViewModel实例,该实例将IDataService接口作为构造函数中的参数。我使用接口将数据服务注入MainViewModel,并可以在Blend中为设

我有一个MainViewModel和Customers属性(CustomerServiceModel对象列表作为ObservableCollection)。我想将该列表作为ItemsSource绑定到ListView控件。我的MainViewModel绑定到窗口的DataContext属性。我使用ServiceLocator模式创建MainViewModel实例,该实例将IDataService接口作为构造函数中的参数。我使用接口将数据服务注入MainViewModel,并可以在Blend中为设计器创建一些测试数据。在Expression Blend中一切正常,数据显示正确,但当我运行代码ItemsSource时,它为null,并且没有数据绑定。DataContext是正确的。当我将DataContext设置为null并再次设置为MainViewModel实例ItemsSource时,它会自动设置为Customers属性。为什么应用程序启动时它不自动绑定到Customers属性

<Grid x:Name="LayoutRoot" DataContext="{Binding MainViewModel, Source={StaticResource Locator}}">
    <ListView Margin="12" x:Name="customerList"
              ItemsSource="{Binding Customers}">
        ...
    </ListView>
</Grid>

...

在创建视图之前,定位器的MainViewModel是否存在?如果稍后填充,并且您没有触发NotifyPropertyChanged事件,则绑定将始终指向初始绑定时的空值。

Locator在创建视图之前创建MainViewModel。但我发现在创建视图之后,我在异步回调上填充Customers属性。因此不会触发NotifyPropertyChanged事件。非常感谢!:-)