2011-09-05 7 views
2

문제는이 게시물에 시작 :이 모델에 MVVM 패턴을 적용하는 방법은 무엇입니까?

List<Container> 
(Below container properties) 
    - Objective: string 
    - Problems: List<ProblemUserControl> 

ProblemUserControls가 문제라는 추가 속성을 contais UserControl이있다 : 나는 이런 식으로 뭔가를 설계했다 Using binding to a List<UserControl> how can I do for not showing the controls

. 하지만 위의 게시물은 MVVM 패턴을 사용하는 것이 좋습니다. 조사 중이지만 여전히 혼란 스럽거나 WPF의 패턴을 이해하는 데 약간의 도움이 필요합니다.

+0

직접 질문에 대답하지 않습니다하지만 일부 사용이 될 수를 보여 당신의 사각형을 주석 한합니다/questions/1939403/mvvm-viewmodel-vs-mvc-viewmodel/1939606 # 1939606 –

답변

1

다음은 MVVM을 사용하여 설명하기위한 예제입니다. 사용자 정의 컨트롤 목록을 가질 필요가 없으며 실제로 이것은 MVVM 관점에서 잘못된 것으로 간주됩니다.

Visual Studio의 기본 WPF 응용 프로그램 템플릿을 기반으로합니다.

여기에 관련된 수업이 있습니다.

public abstract class ViewModelBase : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    protected virtual void OnPropertyChanged(string propertyName) 
    { 
     PropertyChangedEventHandler changed = PropertyChanged; 
     if (changed != null) 
     { 
      changed(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 

public class Container : ViewModelBase 
{ 
    private string m_Objective; 
    private ProblemCollection m_Problems; 

    public Container() 
    { 
     m_Problems = new ProblemCollection(); 
    } 

    public string Objective 
    { 
     get { return m_Objective; } 
     set 
     { 
      m_Objective = value; 
      OnPropertyChanged("Objective"); 
     } 
    } 

    public ProblemCollection Problems 
    { 
     get { return m_Problems; } 
     set 
     { 
      m_Problems = value; 
      OnPropertyChanged("Problems"); 
     } 
    } 
} 

public class Problem : ViewModelBase 
{ 
    private string m_Name; 

    public string Name 
    { 
     get { return m_Name; } 
     set 
     { 
      m_Name = value; 
      OnPropertyChanged("Name"); 
     } 
    } 
} 

public class ProblemCollection : ObservableCollection<Problem> 
{ 
} 

그리고 기본 창. http://stackoverflow.com - 내가 바인딩을

<Window x:Class="StackOverflowDemo.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
    <TextBlock TextWrapping="Wrap" Text="{Binding Objective}" Grid.Column="0" VerticalAlignment="Center" 
     FontWeight="Bold" /> 
    <ItemsControl ItemsSource="{Binding Problems}"> 
     <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <!--<Rectangle Stroke="Black" Height="20" Width="20" Margin="1,0" />--> 
      <TextBlock Text="{Binding Path=Name}" /> 
     </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 
    </Grid> 
</Window> 

MainWindow.cs

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 

     // Create dummy test data. 
     // Normally this will be passed to the window or set externally 
     var container = new Container(); 
     container.Problems.Add(new Problem {Name = "Foo"}); 
     container.Problems.Add(new Problem {Name = "Bar"}); 
     container.Problems.Add(new Problem {Name = "hello"}); 
     container.Problems.Add(new Problem {Name = "world"}); 

     DataContext = container; 
    } 
} 
1

이 패턴은 소프트웨어의 논리 계층 간의 적절한 분리와 종속성 유지에 관한 것입니다. 모델 코드 (Container of Objectives)를 디스플레이 코드 (Usercontrols 목록)와 혼합하기 때문에 표시 논리와 비즈니스 논리가 혼동을 일으킬 수 있습니다.

대신 목표를 유지하고 List<Problem>List<ProblemUserControl> 대신. 그런 다음 WPF 및 바인딩을 사용하여 ProblemUserControlProblem을 연결합니다. 사용자 컨트롤은 Problem이 무엇인지 이해하므로 Problem의 속성에 바인딩 할 수 있습니다. 이렇게하면 레이어를 분리하고 일반적으로 소프트웨어에 대해 쉽게 추론 할 수 있습니다.

+0

그렉, 네가 나에게 말하려고하는 것을 이해할 것 같아. 하지만 어떻게하면 응용 프로그램을 만들 수 있을지 모르겠다. – Darf

+0

한 번에 한 단계 씩. 마법의 총알은 여기에 없습니다 : 단지 그것을 시작하고 실수를 저 지르며 학습하십시오. –

관련 문제