代码之家  ›  专栏  ›  技术社区  ›  Amsakanna

在单个列表视图中显示多个模型

  •  1
  • Amsakanna  · 技术社区  · 14 年前

    现在我已经尝试使用一个集成的ViewModel,它拥有三个模型的所有属性。

    public class IntegratedViewModel
    {
        ContactModel _contactModel;
        NoteModel _noteModel;
    
        public IntegratedViewModel(ContactModel contactModel)
        {
            _contactModel = contactModel;
        } // similarly for other models also
    
        public string DisplayTitle    // For displaying in ListView
        {             
            get
            { 
                If(_contactModel != null) 
                    return _contactModel.Name; 
                If(_noteModel != null)
                    return _noteModel.Title;
            }
        }
    
        //  All other properties from the three models includin the Name/Title properties for displaying them in the corresponding views(UserControl)
    }
    

    List<IntegratedViewModel> .

    我现在必须将视图的可见性绑定到MainViewModel中的一些属性。我试着设置布尔属性,比如 IsContactViewSelected , IsNoteViewSelected SelectedEntity 属性,该属性绑定到ListView的SelectedItem。

    public SelectedEntity
    {
      //get
      set
      {
        oldvalue = _selectedEntity;
        _selectedEntity = value;
        // now i find the Type of model selected using `oldvalue.ModelType`
        // where ModelType is a property in the IntegratedViewModel
        // according to the type, i set one of the above bool properties to false
        // and do the same for _selectedEntity but set the property to true
        // so that the view corresponding to the selectedEntityType is visible 
        // and others are collapsed
      }
    }
    

    [[此处所述的可见性问题已解决]]

    1 回复  |  直到 14 年前
        1
  •  1
  •   Ray Burns    14 年前

    我非常相信MVVM,但我不相信创建视图模型,除非有必要。只要模型对象正确地支持更改通知,并且视图没有视图状态,就可以直接使用模型对象。

    以下是我几年前为解决这个问题而写的:

    <ListView ItemsSource="{Binding AllSelections}">
      <ListView.View>
        <GridView>
    
          <!-- First column -->
          <GridViewColumn Header="Title" DisplayMemberBinding="{Binding}">
            <GridViewColumn.CellTemplate>
              <DataTemplate>
                <DataTemplate.Resources>
    
                  <!-- First column content for ContactModel objects -->                  
                  <DataTemplate DataType="{x:Type local:ContactModel}">
                    <TextBlock Text="{Binding Name}" />
                  </DataTemplate>
    
                  <!-- First column content for NoteModel objects -->                  
                  <DataTemplate DataType="{x:Type local:NoteModel}">
                    <TextBlock Text="{Binding Title}" />
                  </DataTemplate>
    
                  ...
    
                </DataTemplate.Resources>
    
                <!-- This selects one of the above templates and applies it -->
                <ContentPresenter /> 
    
              </DataTemplate>
            </GridViewColumn.CellTemplate>
          </GridViewColumn>
    
          <!-- Second column -->
          <GridViewColumn ...>
            ...
          </GridViewColumn>
    
        </GridView>
      </ListView.View>
    </ListView>
    

    其中,“AllSelections”是ViewModel中的一个属性,它包含一个ICollection,该ICollection包含ContactModel、NoteModel和ReminderModel对象的混合体,还实现INotifyCollectionChanged。

    这种实现视图的方法非常清晰,可以方便地自定义各种对象类型的表示。


    <!-- First column -->
    <GridViewColumn Header="Title"
                    DisplayMemberBinding="{edf:ExpressionBinding
                      context is local:ContactModel ? Name : Title}" />
    
    <!-- Second column -->
    <GridViewColumn ... />
    

    我希望不久能发布我的图书馆给其他人使用,或者你也可以自己写。同时,使用多个数据模板的解决方案比创建ViewModel并在其中镜像属性要干净得多。