簡體   English   中英

在單個ListView中顯示多個模型

[英]Showing multiple models in a single ListView

我有三種模型(聯系人,便箋,提醒)。 我想搜索所有這些,並在單個listview中生成過濾后的結果,並且根據選擇,我必須在其右側顯示相應的view(UserControl)。

我想要正確的方法來實現我嘗試過的方法的設計或至少替代方法。

現在,我使用了IntegratedViewModel進行了嘗試,該模型具有所有三個模型的所有屬性。

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)
}

現在,將itemsSource設置為List<IntegratedViewModel>

現在,我必須將視圖的可見性綁定到MainViewModel中的某些屬性。 我嘗試使用綁定到ListView的SelectedItem的SelectedEntity屬性的設置IsNoteViewSelected來設置IsContactViewSelectedIsNoteViewSelected類的布爾屬性。

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
  }
}

[[此處所述的可見性問題已解決]]

我堅信MVVM,但除非必要,否則我不相信創建視圖模型。 只要您的模型對象正確支持更改通知並且視圖沒有視圖狀態,您就可以直接使用模型對象。

就是這種情況。 您的ListView可以直接綁定到模型而不會造成混亂,所以這就是我要遵循的方法。

這是我幾年前為解決此問題而寫的內容:

<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。

這種實現視圖的方式非常清晰,可以輕松自定義各種對象類型的表示形式。


今天,我使用我編寫的名為Emerald Data Foundation的庫,該庫使處理基於類的源屬性名稱的轉換變得更加容易:

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

<!-- Second column -->
<GridViewColumn ... />

我希望不久后發布我的庫,以供其他人使用,或者您可以編寫自己的庫。 同時,具有多個DataTemplates的解決方案可以工作,並且比創建ViewModel並在其中鏡像屬性要干凈得多。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM