问题
I have a MVVM Light View Model with a ListBox bound to a List<>. The Listbox gets populated initially just fine, but when I change to a different list at runtime (not just adding items), it does not get updated unless I set the specific Data context.
So this works:
...
DataContext="{Binding MyViewModel, Source={StaticResource Locator}}"
<ListBox ItemsSource="{Binding MyList}"/>
But this doesn't:
DataContext="{Binding Source={StaticResource Locator}}"
<ListBox ItemsSource="{Binding MyViewModel.MyList}"/>
In both cases, I can step through the set call to verify that the RaisePropertyChanged method is being called properly, but in the second case, an associated "get" never happens.
Is this working as designed, or is this a bug in MVVM-Light or possibly Silverlight?
Locator:
public class ViewModelLocator
{
private static UnityContainer Container;
static ViewModelLocator()
{
Container = new UnityContainer();
Container.RegisterType<MainViewModel>(new ContainerControlledLifetimeManager());
}
public MyViewModelType MyViewModel
{
get
{
return Container.Resolve<MyViewModelType>();
}
}
来源:https://stackoverflow.com/questions/6602083/bindings-not-updating-without-specific-datacontext