2011-09-05 31 views
2

問題始於這個帖子:Using binding to a List<UserControl> how can I do for not showing the controls如何在此模型中應用MVVM模式?

我設計是這樣的:

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

ProblemUserControls是一個用戶控件,其中contais稱爲問題的一個額外的屬性。但上面的帖子一個人建議我使用MVVM模式。我正在調查,但是,我仍然感到困惑,或者我需要一些幫助來理解WPF中的模式。

+0

並不直接回答這個問題,但可能是一些使用 - http://stackoverflow.com/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> 
{ 
} 

和主窗口。請注意,我註釋掉您的矩形顯示綁定

<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

該模式是關於在您的軟件的邏輯層之間保持適當的分離和依賴關係。您在示例中將顯示邏輯與業務邏輯混淆在一起,因爲您正在將您的模型代碼(目標容器)與您的顯示代碼(用戶控件列表)混合在一起。

而是保持目標並保持List<Problem>而不是List<ProblemUserControl>。然後使用WPF和綁定關聯您的ProblemUserControlProblem。您的用戶控件瞭解Problem是什麼,因此您可以綁定Problem上的屬性。通過這種方式,您可以分離圖層,並且可以更容易地推理您的軟件。

+0

Greg,我猜我明白你想告訴我什麼。但我不知道如何開始構建應用程序 – Darf

+0

一步一個腳印。這裏沒有魔力:開始做,犯錯誤,並從中吸取教訓。 –