本文介绍了在 MVVM 中为 WPF 实现一个简单的 Master-Detail 场景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 MVVM 的 WPF 应用程序.我有一些用户控件应该使用简单的数据绑定在 3 个文本框控件中显示一个人的名字、姓氏和电子邮件.

I have a WPF application using MVVM. I have some user controls that should show a Person FirstName, LastName and email in 3 Textbox controls using simple databinding.

用户控件有一个简单的组合框,用户可以在其中选择用户的 ID,因此应加载具有该 ID 的人员记录(从数据库中获取其数据),然后名字、姓氏和电子邮件将显示在文本框.

The User Control has a simple combobox where the user selects the ID for the user and therefore the Person Record with that ID should be loaded (its data fetched from the database) and then the FirstName, LastName and Email will display in the textBoxes.

我有一个包含 ID 组合框的用户控件和三个属性的 3 个文本框,一个 ViewModel 类和一个具有三个属性(名字、姓氏和电子邮件)的模型类(人物类).

I have a Usercontrol with the combobox for the IDs and 3 textboxes for the three properties, a ViewModel Class and a Model class (person Class) with the three properties (FirstName, LastName and Email).

使用 MVVM(最好)实现此行为的最简单方法是什么?有样品吗?

What is the simplest way to implement this behavior using MVVM(preferably)? any samples?

推荐答案

我猜这里是因为您的问题有点含糊,您不太确定如何将各个部分连接在一起.为简单起见,让我们将 ViewModel 直接挂接到用户控件上,并将其全部绑定.

I'm guessing here since your question is a little vague that you're not quite sure how to hook the pieces together. For simplicity's sake let us hook the ViewModel directly to the user control and get it all binding.

只要您的视图模型填充了正确的 People 集,下面的所有绑定都会处理数据并显示正确的数据.请注意组合框中所选项目的双向绑定.这允许 WPF 将新的选定项发送回视图模型.

As long as your view model is populated with the correct set of People, all the binding below will handle the data and show the correct data. Take note of the two-way binding for the selected item in the combobox. That allows WPF to send back the new selected item to the viewmodel.

在UserControl后面的代码中:

In the UserControl's code behind:

public MyUserControl()
{
  DataContext = new MyViewModel();
}

在 UserControl 的 XAML 中:

In the UserControl's XAML:

<ComboBox ItemsSource="{Binding AllPeople}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}" />
<TextBox Text="{Binding SelectedItem.LastName}" />
<TextBox Text="{Binding SelectedItem.FirstName}" />
<TextBox Text="{Binding SelectedItem.EmailName}" />

您的视图模型:

private IEnumerable<Person> _allPeople;
    public IEnumerable<Person> AllPeople
    {
        get { return _allPeople; }
        set
        {
            if (_allPeople != value)
            {
                _allPeople = value;
                NotifyPropertyChanged("AllPeople");
            }
        }
    }

    private Person _selectedItem;
    public Person SelectedItem
    {
        get { return _selectedItem; }
        set
        {
            if (!_selectedItem != value)
            {
                _selectedItem = value;
                NotifyPropertyChanged("SelectedItem");
            }
        }
    }

    private void NotifyPropertyChanged(string propertyName)
    {
       if ( PropertyChanged != null)
       {
         PropertyChanged(this, new PropertyChangedEventArgs(propertyName);
       }
    }
}

public class Person
{
 public int PersonId { get; set; }
 public string FirstName { get; set; }
 public string LastName { get; set; }
 public string Email { get; set; }
}

这篇关于在 MVVM 中为 WPF 实现一个简单的 Master-Detail 场景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 18:32