【问题标题】:UWP bind a ListView's item as UserControl DataTemplateUWP 将 ListView 的项目绑定为 UserControl DataTemplate
【发布时间】: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


    【解决方案1】:

    如果您实际上正在寻找允许您根据数据类型将两个不同的数据模板加载到视图模型的数据绑定概念,那么您绝对应该查看以下视频。

    我在我的一个应用程序中实现了相同的功能,我不想在此处发布任何代码,因为它只是另一种复制粘贴解决方案。

    See This Video

    如果您对 DataTemplateSelector 的解释有任何问题,请告诉我。

    【讨论】:

    • 感谢您的回答,也许我错了,但是当我使用两个 x:DataType 不同的 dataTemplate 时,我不需要 datatempalteselector 吗?我的 ObservableCollection 仅使用两个 DataType,而我的 SelectedProfil 是 TeacherInfosProfilVM 或 StudentInfosProfilVM 。我的问题是为什么我不能使用正确的 dataTempalte 访问 Nom 或其他属性? (而是显示 dataTempalte 的名称)
    • 谢谢,DataTemplateSelector 解决了我的问题 =)
    猜你喜欢
    • 2019-04-13
    • 2016-03-23
    • 2019-09-29
    • 2017-02-18
    • 2016-04-18
    • 1970-01-01
    • 1970-01-01
    • 2020-02-01
    • 2017-09-18
    相关资源
    最近更新 更多