简体   繁体   English

Wpf 开多 Window Mvvm

[英]Wpf Open multi Window Mvvm

first sorry for my English首先对不起我的英语

i have class person that has some properties我有 class 人有一些属性

class Person
{
    //Properties
}

and Window Person Search this is View Model of this window contain list of person and use Mvvm和 Window 人员搜索这是查看此 window 的人员列表并使用 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和 Window 要插入和更新的人员信息

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和这个 Window 的视图 Model

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我需要在打开 Window 时从 Window 人员信息和选定人员项目中搜索...更改自动属性 ===> PersonInfo实现 class 是一项责任

Your problem is that your are driving everything from the view.您的问题是您正在从视图中驱动所有内容。 In MVVM it should be the VMs that are charge and driving everything.在 MVVM 中,它应该是负责和驱动一切的 VM。 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 .目前您正在从PersonInformationView创建PersonSearchView ,但这使您看不到PersonSearchViewModel内的PersonSearchView ,并且它是具有SelectedPersonPersonSearchViewModel

Instead you should be creating PersonSearchViewModel from PersonInformationViewModel .相反,您应该从PersonInformationViewModel创建PersonSearchViewModel 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.这样,您可以对PersonSearchViewModel.SelectedPerson中的更改做出反应,方法是订阅PropertyChanged ,或者通过将回调委托传递给PersonSearchViewModel构造函数,当一个人选择一个人时它应该调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM