Bindings Not Updating without Specific DataContext

回眸只為那壹抹淺笑 提交于 2019-12-09 23:57:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!