2011-09-05 121 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

一步一个脚印。这里没有魔力:开始做,犯错误,并从中吸取教训。 –