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