在MVVM中为WPF实现简单的Master-Detail方案 [英] Implementing a simple Master-Detail scenario for WPF in MVVM

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

问题描述

我有一个使用MVVM的WPF应用程序。我有一些用户控件,应该使用简单的数据绑定在3个Textbox控件中显示一个Person FirstName,LastName和电子邮件。



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



我有一个用户控件,包含三个属性的ID和3个文本框的组合框,一个具有三个属性(FirstName,LastName和Email)的ViewModel类和Model类(person类)。



使用MVVM实现此行为的最简单方法是什么(最好)?任何样本?

解决方案

我猜这里是因为你的问题有点模糊,你不太确定如何钩这些作品在一起。为了简单起见,我们可以直接将ViewModel挂接到用户控件中,并将其全部绑定。



只要您的视图模型填充了正确的一组人,所有下面的绑定将处理数据并显示正确的数据。记下组合框中所选项目的双向绑定。这允许WPF将新选择的项目发送回viewmodel。



在UserControl的代码中:

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

在UserControl的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}/>

您的ViewModel:

  private IEnumerable< Person> _所有人; 
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;}
}


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.

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.

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).

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

解决方案

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.

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.

In the UserControl's code behind:

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

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}" />

Your ViewModel:

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方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆