first sorry for my English
i have class person that has some properties
class Person
{
//Properties
}
and Window Person Search this is View Model of this window contain list of person and use Mvvm
class PersonSearchViewModel
{
public PersonSearchViewModel(Person person)
{
SelectedPerson = person;
}
public Person SelectedPerson { set; get; }
public ObservableCollection <Person> PersonList { set; get; }
}
and Window Person Information to insert and update
class PersonInformationView : Window
{
public PersonInformationView()
{
InitializeComponent();
this.DataContex = vm;
}
PersonInforamtionViewModel vm = new PersonInforamtionViewModel();
private void buttonOpenSearch_Click(object sender, RoutedEventArgs e)
{
PersonSearchView p = new PersonSearchView(vm.PersonInfo);
}
}
and the View Model Of this Window
class PersonInforamtionViewModel
{
public Person PersonInfo { set; get; }
}
i need when open Window search from Window Person Information and selected person item... Change Auto the property ===> PersonInfo To achieve the class is one responsibility
Your problem is that your are driving everything from the view. In MVVM it should be the VMs that are charge and driving everything. The views are just wrappers around the VMs
Currently you are creating PersonSearchView
from PersonInformationView
, but this gives you no visibility on the PersonSearchViewModel
inside PersonSearchView
, and it's PersonSearchViewModel
that has SelectedPerson
.
Instead you should be creating PersonSearchViewModel
from PersonInformationViewModel
. That way you can react to changes in PersonSearchViewModel.SelectedPerson
, either by subscribing to PropertyChanged
, or probably better by passing in a callback delegate into the PersonSearchViewModel
constructor that it should call when a person selects a person.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.