Silverlight에서 내 MVVM 예제를 만들기위한 기초 자료로 Model View View-Model (MVVM) in Silverlight 문서를 사용하고 있습니다.이 Silverlight MVVM 예제에서 ModelView를 모델에 어떻게 연결할 수 있습니까?
나는 아래의 모든 조각을 가지고 :
- 메인 페이지 (로드 모두)
- 보기 (바인딩 XAML 파일)
- 모델 (고객의 클래스 생성 가짜 List 컬렉션)
- ModelView (상속 INotifyPropertyChanged 및 ha 하여 PropertyChanged 내 메인 페이지에서보기가 필요로하는 두 개의 필드)
에 대한 I S :
- 는 의 DataContext에에 뷰 모델을
- 바인드 뷰 모델를 만들 보기
- 모델 (custo mers)
하지만 이제 ModelView를 모델에 어떻게 연결합니까? 마치 CustomerViewModel에 고객 모델을 삽입해야한다고 생각합니다. 맞습니까? 하지만 정확히 어떻게? 이 MVVM 예제를 마치기 위해 다음 단계는 무엇입니까? MVVM 패턴의 장점을 사용할 수 있도록 예를 들어 보겠습니다. 등, 테스트 모델로 모델을 교체 새보기와보기를 교환
MainPage.xaml.cs를 :는 뷰 모델을 생성 뷰 모델보기를 부착
using System.Windows.Controls;
using System.Collections.Generic;
namespace TestMvvm345
{
public partial class MainPage : UserControl
{
private CustomerViewModel customerData;
public MainPage()
{
InitializeComponent();
customerData = new CustomerViewModel();
customerHeaderView.DataContext = customerData;
List<Customer> customers = Customer.GetCustomers();
}
}
}
에서 MainPage.xaml :표시 메인 페이지
012,369,436,644의 컨텍스트 내에서보기CustomerViewModel.xaml :뷰 모델
using System.ComponentModel;
namespace TestMvvm345
{
public class CustomerViewModel : INotifyPropertyChanged
{
private string firstName;
private string lastName;
public string FirstName
{
get { return firstName; }
set
{
firstName = value;
RaisePropertyChanged("FirstName");
}
}
public string LastName
{
get { return lastName; }
set
{
lastName = value;
RaisePropertyChanged("LastName");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
}
CustomerHeaderView.xaml보기
<UserControl x:Class="TestMvvm345.CustomerHeaderView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
<StackPanel HorizontalAlignment="Left">
<ListBox x:Name="CustomerList" ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock
Text="{FirstName}"/>
<TextBlock
Text="{LastName}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Grid>
</UserControl>
고객.CS모델
using System;
using System.Collections.Generic;
namespace TestMvvm345
{
public class Customer
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int NumberOfContracts { get; set; }
public static List<Customer> GetCustomers()
{
List<Customer> customers = new List<Customer>();
customers = new List<Customer>();
customers.Add(new Customer { FirstName = "Jim", LastName = "Smith", NumberOfContracts = 23 });
customers.Add(new Customer { FirstName = "Jane", LastName = "Smith", NumberOfContracts = 22 });
customers.Add(new Customer { FirstName = "John", LastName = "Tester", NumberOfContracts = 33 });
customers.Add(new Customer { FirstName = "Robert", LastName = "Smith", NumberOfContracts = 2 });
customers.Add(new Customer { FirstName = "Hank", LastName = "Jobs", NumberOfContracts = 5 });
return customers;
}
}
}