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