如果没有特定的 DataContext,绑定不会更新
我有一个 MVVM Light View Model,其中 ListBox 绑定到 List<>。列表框最初填充得很好,但是当我在运行时更改为不同的列表(不仅仅是添加项目)时,它不会更新,除非我设置特定的数据上下文。
所以这有效:
...
DataContext="{Binding MyViewModel, Source={StaticResource Locator}}"
<ListBox ItemsSource="{Binding MyList}"/>
但这并非如此:
DataContext="{Binding Source={StaticResource Locator}}"
<ListBox ItemsSource="{Binding MyViewModel.MyList}"/>
在这两种情况下,我都可以单步执行 set 调用来验证 RaisePropertyChanged 方法是否被正确调用,但在第二种情况下,关联的“get”永远不会发生。
这是按设计工作的吗,还是 MVVM-Light 或 Silverlight 中的错误?
定位器:
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>();
}
}
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>();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论