2011-09-08 80 views
0

我可能会错过简单的东西,请耐心等待。 我有一个包含以下内容的视图模型:从Child ViewModels接收通知

public ObservableCollection<Person> PersonCollection 
{ 
    get { return personCollection; } 
    set 
    { 
     if (personCollection != value) 
     { 
      personCollection = value; 
      RaisePropertyChanged("PersonCollection"); 
     } 
    } 
} 

然后在另一个视图模型我:

public ObservableCollection<Person> PersonCollection 
{ 
    get 
    { 
     PersonViewModel vm = (App.Current.Resources["Locator"] as ViewModelLocator).PersonViewModel; 
     return vm.PersonCollection; 
    } 
} 

public PersonViewModel PersonViewModel 
{ 
    get 
    { 
     return ((App.Current.Resources["Locator"] as ViewModelLocator).PersonViewModel) 
    } 
} 

在我的XAML,如果我绑定到PersonCollection然后更新不上我的看法发生,但如果我绑定到PersonViewModel.PersonCollection它。那么这是做到这一点的“正确”方式,还是有一种方法让视图使用第一种方法检测通知?

回答

1

更改绑定到{Binding PersonViewModel.PersonCollection}

你的包裹PersonCollection属性没有变化的通知,所以视图不知道该属性已经改变(这当然没有办法知道它最初来自PersonViewModel以从得到变化通知它

+0

啊我明白你在说什么。我想这在你思考时很明显。因此,在ViewModel中使用实体的方式是根据我的第二种方法在父级内提供子ViewModel的属性,然后通过该方法进行绑定? – Firedragon