开发者

Bindings Not Updating without Specific DataContext

开发者 https://www.devze.com 2023-03-18 07:10 出处:网络
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

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>();
            }
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消