2016-07-26 98 views
0

我嘗試實現主/細節視圖,並嘗試瞭解如何將所選項綁定到具有兩個DataTemplate的UserControl中。UWP將ListView的項綁定爲UserControl DataTemplate

我有兩種型號(我會打電話給他們教師和學生)

我的視圖模型:

public class Page_ProfilVM : NotificationBase 
{ 
    public ObservableCollection <AbstractInfosProfilVM> InfosProfil { get; set; } 
    private AbstractInfosProfilVM selectedProfil; 
    public AbstractInfosProfilVM SelectedProfil 
    { 
     get { return selectedProfil; } 
     set 
     { 
      SetProperty(selectedProfil, value,() => selectedProfil = value); 
     } 
    } 
} 

public abstract class AbstractInfosProfilVM : NotificationBase 
{ 
    private string nom; 
    public string Nom 
    { 
     get { return nom; } 
     set { SetProperty(nom, value,() => nom = value); } 
    } 
} 

public class TeacherInfosProfilVM : AbstractInfosProfilVM 
{ 
} 
public class StudentInfosProfilVM : AbstractInfosProfilVM 
{ 
} 

我正確顯示主視圖

<!-- ListView --> 
<ListView ItemSource="{x:bind ViewModel.Profils}" 
      SelectionMode="Single" 
      SelectedItem="x:bind ViewModel.SelectedProfil, Mode="TwoWay", Converter={.....}}"> 

<ListView.ItemTemplate> 
    <DataTemplate x:DataType="vm:AbstractProfilVM"> 
     <!-- Master --> 
     <widget:CelProfilMaster CelProfilMasterName={x:Bind Name} CelProfilMasterAge={x:Bind Age} ... /> 
    </DataTemplate> 
</ListView.ItemTemplate> 

而且我正確地顯示詳細視圖(具有依賴項屬性的用戶控件)上所選項目的詳細信息。但是現在我需要選擇正確的dataTempalte來顯示教師的屬性和學生的屬性。但它不起作用。

<!-- Details--> 
<widget:CelluleProfilDetails x:Name = "DetailContent" 
          CelluleProfilDetailsNom = "{x:Bind ViewModel.SelectedProfil.Nom,Mode=TwoWay,Converter={StaticResource GenericConverter}}" 
          CelluleProfilDetailsPrenom = "{x:Bind ViewModel.SelectedProfil.Prenom, Mode=TwoWay,Converter={StaticResource GenericConverter}}" > 

<widget:CelluleProfilDetails.CelluleProfilDetailsContent> 
    <ContentPresenter Content="{x:Bind ViewModel.SelectedProfil,Mode=OneWay}"> 

    <ContentPresenter.Resources> 
     <DataTemplate x:DataType="viewModels:TeacherInfosProfilVM" x:Name="T1" > 
      <TextBlock Text="{x:Bind Nom}"/> 
     </DataTemplate> 
     <DataTemplate x:DataType="viewModels:StudentInfosProfilVM" x:Name="T2" > 
      <TextBlock Text="{x:Bind Nom}"/> 
     </DataTemplate> 
    </ContentPresenter.Resources> 

    </ContentPresenter>       
</widget:CelluleProfilDetails.CelluleProfilDetailsContent> 

</widget:CelluleProfilDetails> 

我嘗試顯示教師/學生的名稱,但它只顯示視圖模型的名稱。 如何正確地將老師的屬性和學生的屬性顯示爲「CelluleProfilDetails.CelluleProfilDetailsContent」?

回答

1

如果你實際上是尋找數據綁定的概念,讓您加載基於數據類型到View模式兩種不同的數據模板,你一定要看看下面的視頻。

我實現了同我的應用程序之一,我不想在這裏發佈的任何代碼,因爲它會只是一個複製粘貼的解決方案。

See This Video

如果你有DataTemplateSelector說明任何問題,讓我知道。

+0

謝謝您的回答, 也許我有錯,但是當我使用兩個DataTemplate中有兩個X:數據類型不同,我不需要datatempalteselector? 我的ObservableCollection只使用兩個DataType,而我的SelectedProfil是一個TeacherInfosProfilVM或一個StudentInfosProfilVM。 我的問題是爲什麼我無法使用正確的dataTempalte訪問Nom或其他屬性? (而不是顯示dataTempalte的名稱) – sasukaru

+0

謝謝,DataTemplateSelector已經解決了我的問題=) – sasukaru

相關問題