C# 如何在这个模型中应用MVVM模式?
问题从这篇文章开始: 我在设计这样的东西:C# 如何在这个模型中应用MVVM模式?,c#,wpf,design-patterns,C#,Wpf,Design Patterns,问题从这篇文章开始: 我在设计这样的东西: List<Container> (Below container properties) - Objective: string - Problems: List<ProblemUserControl> 列表 (容器属性下方) -目标:字符串 -问题:列表 ProblemUserControls是一个UserControl,其中包含一个名为Problem的额外属性。但是上面的帖子有人建议我使用MVVM模式。我
List<Container>
(Below container properties)
- Objective: string
- Problems: List<ProblemUserControl>
列表
(容器属性下方)
-目标:字符串
-问题:列表
ProblemUserControls是一个UserControl,其中包含一个名为Problem的额外属性。但是上面的帖子有人建议我使用MVVM模式。我正在调查,但是我仍然感到困惑,或者我需要一些帮助来理解WPF中的模式。该模式是关于在软件的逻辑层之间保持适当的分离和依赖性。在您的示例中,您将显示逻辑与业务逻辑混淆,因为您将模型代码(目标的容器)与显示代码(用户控件的列表)混合在一起
相反,保留您的目标,并维护一个
列表,而不是列表
。然后使用WPF和绑定将您的ProblemUserControl
与问题关联起来。您的用户控件了解什么是问题
,因此您可以根据问题
上的属性进行绑定。通过这种方式,您可以分离各个层,从而更容易对软件进行总体分析。下面是一个使用MVVM的示例。请注意,不需要用户控件列表,从MVVM的角度来看,这确实是不正确的
这基于VisualStudio中的默认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>
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;
}
}