【发布时间】:2016-11-30 06:24:46
【问题描述】:
我尝试实现主/详细视图,并尝试了解如何将所选项目绑定到具有两个 DataTemplate 的 UserControl。
我有两种模型(我称他们为老师和学生)
我的视图模型:
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”中?
【问题讨论】:
标签: c# xaml mvvm datatemplate datacontext