ViewModel包含另一个ViewModel的集合 [英] ViewModel contains collection of another ViewModel
问题描述
在所有有关MVVM的教程中,我读到 View
不应该了解 Model
,因此引入了 ViewModel
层.
In all tutorial about MVVM I read that View
should not know about Model
, hence ViewModel
layer is introduced.
如果是这样的情况:例如,我们有 Shelf
,其中包含 Pack
:
If so in situation like: we have Shelf
which contains Pack
:
namespace Storage.Model
{
class Pack
{
public string Name { get; set; }
}
}
<UserControl x:Class="Storage.View.Shelf" ... >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="20" />
<RowDefinition Height="50" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="{Binding Path=Name}"/>
<TextBlock Grid.Row="1" Text="{Binding Path=Capability}"/>
<ItemsControl Grid.Row="2" ItemsSource="{Binding Path=Packs}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border>
<TextBlock Text="{Binding Name}" />
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</UserControl>
// DataContext for Shelf
namespace Storage.ViewModel
{
public class ShelfViewModel
{
public string Name { get; set; }
public string Capability { get; set; }
// It's okay that ViewModel contains collection of another ViewModel?
public ObservableCollection<PackViewModel> Packs { get; set; }
}
}
// DataContext for Pack
namespace Storage.ViewModel
{
class PackViewModel
{
private Pack pack;
public string Name
{
get{ return pack.Name; }
set{ ... }
}
public PackViewModel(int id)
{
// How to populate reference to Pack instance?
pack = (new PackRepository()).getById(id)
}
}
}
就像我在上面的代码注释中提到的那样,一个ViewModel创建另一个ViewModel的实例还可以吗?我可以想象一下其中有 Storage
和 Shelf
集合的情况,因此我们最终得到对ModelView的级联引用: StoreViewModel
包含 ObservableCollection< ShelfViewModel>
包含 ObservableCollection< PackViewModel>
的集合.
Like I mention in code comment above, it okey that one ViewModel create instances of another ViewModel? I can image case where we have Storage
with collection of Shelf
, hence we end up with cascading reference to ModelView: StoreViewModel
contains ObservableCollection<ShelfViewModel>
which cotains collection of ObservableCollection<PackViewModel>
.
该解决方案出现的另一个问题是,如何参照 Pack
类填充新引入的 PackViewModel
?我们必须以某种方式将唯一标识符传递给 PackViewModel
实例.
Another problem with arise with that solution is that how to populate newly introduced PackViewModel
with reference to Pack
class? We must somehow pass unique identifier to PackViewModel
instance.
I don't want hide that this question is related to my other question Shoud view layer know about model classes (entities)?
推荐答案
就像我在上面的代码注释中提到的那样,一个ViewModel可以创建另一个ViewModel的实例吗?
Like I mention in code comment above, it okey that one ViewModel create instances of another ViewModel?
是的.在这种情况下, PackViewModel
只是 Pack
的包装.例如,当您不想直接公开或绑定到模型类型时,或者当模型类型未实现 INotifyPropertyChanged
接口时,通常使用此类包装器.
Yes. In this case PackViewModel
is just a wrapper around Pack
. You typically use such a wrapper when you don't want to expose or bind to the model type directly or when the model type doesn't implement the INotifyPropertyChanged
interface for example.
这很好.实际上,您最终将获得更多的课程,但是每个课程都有其自己的责任.例如,您可以使用特定于UI的属性来扩展包装器类,即,绑定到该视图的属性通常都存在.
This is perfectly fine. You will indeed end up with more classes, but each class has its own responsibility. You could for example extend the wrapper class with properties that are specific to the UI, i.e. properties that are obly there for the view to bind to.
该解决方案出现的另一个问题是,如何使用
Pack
类填充新引入的PackViewModel?我们必须以某种方式将唯一标识符传递给PackViewModel
实例
Another problem with arise with that solution is that how to populate newly introduced PackViewModel with reference to
Pack
class? We must somehow pass unique identifier toPackViewModel
instance
您可以在创建包装类时为其添加包装对象:
You could just inject the wrapper class with the wrapped object when you create it:
class PackViewModel
{
private readonly Pack _pack;
public PackViewModel(Pack pack)
{
_pack = pack;
}
public string Name
{
get { return _pack.Name; }
}
}
鉴于您可能从某种存储库或服务中收到的 Pack
对象的集合,则可以轻松地创建包装对象,例如:
Given a collection of Pack
objects that you may have received from some kind of repository or service, you could then easily create wrapper objects, e.g.:
var repo = PackRepository();
var packs = repo.GetPacks();
var wrapperObjects = packs.Select(pack => new PackViewModel(pack));
这篇关于ViewModel包含另一个ViewModel的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!