代码之家  ›  专栏  ›  技术社区  ›  Sonny D

ViewModel包含另一个ViewModel的集合

  •  0
  • Sonny D  · 技术社区  · 8 年前

    在所有关于MVVM的教程中,我都读到了 View 不应该知道 Model 因此 ViewModel 介绍了层。

    如果是这样,在这样的情况下:我们有 Shelf 其中包含 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 包含的集合 架子 ,因此,我们以对ModelView的级联引用结束: StoreViewModel 包含 ObservableCollection<ShelfViewModel> 其中包含 ObservableCollection<PackViewModel> .

    该解决方案的另一个问题是如何填充新引入的 PackViewModel 参考 收拾 班我们必须以某种方式将唯一标识符传递给 PackViewModel 例子

    我不想隐瞒这个问题与我的另一个问题有关 Shoud view layer know about model classes (entities)?

    1 回复  |  直到 8 年前
        1
  •  2
  •   mm8    8 年前

    就像我在上面的代码注释中提到的,一个ViewModel可以创建另一个ViewModel的实例吗?

    对在这种情况下 PackViewModel 只是一个包装 Pack . 当您不想直接公开或绑定到模型类型时,或者当模型类型没有实现 INotifyPropertyChanged 例如,接口。

    这很好。你最终会得到更多的类,但每个类都有自己的责任。例如,您可以使用特定于UI的属性来扩展包装器类,即视图绑定到的对象属性。

    该解决方案出现的另一个问题是,如何填充新引入的PackViewModel,参考 收拾 班我们必须以某种方式将唯一标识符传递给 PackViewModel 例子

    您可以在创建包装类时将包装对象注入包装类:

    class PackViewModel
    {
        private readonly Pack _pack;
        public PackViewModel(Pack pack)
        {
            _pack = pack;
        }
    
        public string Name
        {
            get { return _pack.Name; }
        }
    }
    

    给定的集合 收拾 对象,然后可以轻松创建包装器对象,例如:

    var repo = PackRepository();
    var packs = repo.GetPacks();
    var wrapperObjects = packs.Select(pack => new PackViewModel(pack));