通用 windows phone 应用程序。以编程方式使用 DataTemplate 填充 hubsection

Universal windows phone app. Populate hubsection with DataTemplate programmatically

我的页面 DataTemplate 是这样的:

    <DataTemplate x:Key="msgTemplate">
        <StackPanel>
            <TextBlock Text="Titolo" Foreground="White" Margin="0,10,0,0" FontSize="20"/>
            <TextBlock Text="{Binding TITOLO}" x:Name="titolo" Foreground="White" Margin="0,0,0,0" FontStyle="Italic" TextWrapping="Wrap" FontSize="25"/>

            <TextBlock Text="Descrizione" Foreground="White" Margin="0,10,0,0" FontSize="20"/>
            <TextBlock Text="{Binding DESCRIZIONE}" x:Name="descrizione" Foreground="White" Margin="0,0,0,0" FontStyle="Italic" TextWrapping="Wrap" FontSize="25"/>

            <TextBlock Text="Priorità" Foreground="White" Margin="0,10,0,0" FontSize="20"/>
            <TextBlock Text="{Binding PRIORITA}" x:Name="priorita" Foreground="White" Margin="0,0,0,0" FontStyle="Italic" TextWrapping="Wrap"  FontSize="25"/>

            <TextBlock Text="Visibile dal:" Foreground="White" Margin="0,10,0,0" FontSize="20"/>
            <TextBlock Visibility="{Binding VISIBILE_DA_VISIBILITY}" Text="{Binding VISIBILE_DA}" x:Name="visibileDa" Foreground="White" Margin="0,0,0,0" FontStyle="Italic" TextWrapping="Wrap" FontSize="25"/>

            <TextBlock Text="Visibile al:" Foreground="White" Margin="0,10,0,0" FontSize="20"/>
            <TextBlock Visibility="{Binding VISIBILE_A_VISIBILITY}" Text="{Binding VISIBILE_A}" x:Name="visibileA"  Foreground="White" Margin="0,0,0,0" FontStyle="Italic" TextWrapping="Wrap" FontSize="25"/>
        </StackPanel>
    </DataTemplate>

此数据模板引用此中心:

    <Hub x:Name="Panorama" Grid.Row="1" Width="Auto" Loaded="Panorama_Loaded" SectionsInViewChanged="Panorama_SectionsInViewChanged" >

        <HubSection x:Name="section">
            <DataTemplate>
            </DataTemplate>
        </HubSection>

    </Hub>

我的问题是如何绑定我的数据来填充这个 hubsection。 我希望我的应用程序自动创建 x 个部分,其中 x 是我列表中的项目数。

这是我的对象

    class LSK_MSG
    {
        public Guid ID { get; set; }
        public string TITOLO { get; set; }
        public string DESCRIZIONE { get; set; }
        public string VISIBILE_DA { get; set; }
        public string VISIBILE_A { get; set; }
        public string VISIBILE_DA_VISIBILITY { get; set; }
        public string VISIBILE_A_VISIBILITY { get; set; }
    }

在这里我将模板设置为我正在创建的中心部分

        msgs = new MESSAGGIO().SelectAll();
        lskMsgs = new List<LSK_MSG>();
        maxIndex = msgs.Count;

        HubSection mHubSection;
        foreach (MESSAGGIO m in msgs)
        {
            mHubSection = new HubSection();
            mHubSection.Template = (ControlTemplate)App.Current.Resources["msgTemplate"];

            lskMsgs.Add(new LSK_MSG()
            {
                DESCRIZIONE = m.DESCRIZIONE,
                TITOLO = m.TITOLO,
                ID = m.ID,
                VISIBILE_DA = m.VISIBILE_DA == null ? "" : msg.VISIBILE_DA.Value.ToString("dd/MM/yyyy"),
                VISIBILE_A = m.VISIBILE_A == null ? "" : msg.VISIBILE_A.Value.ToString("dd/MM/yyyy"),
                VISIBILE_DA_VISIBILITY = m.VISIBILE_DA == null ? "Collapsed" : "Visible",
                VISIBILE_A_VISIBILITY = m.VISIBILE_A == null ? "Collapsed" : "Visible"
            });
            mHubSection.s
        }

集线器已初始化,我的问题是:现在我创建了一个 hubsection,现在我将其设置为 DataTemplate。如何将我的“LSK_MSG”设置为 hubsection 的内容?

Pier Giorgio,您需要将每个HubSection 的DataContext 设置为itemlist 的每个对象。由于您是手动创建 HubSection,因此对于列表中的 x 个项目,将创建 x 个 hubsection。此外,您不需要新列表 lskMsgs。只需修改 foreach 循环即可。假设您的 Hub 控件的名称是 testHubControl。

foreach(MESSAGIO m in msgs)
{
  mHubSection = new HubSection();
  mHubSection.ContentTemplate =(DataTemplate)this.Resources["msgTemplate"];
  mHubSection.DataContext = m;
  testHubControl.Sections.Add(mHubSection);
}

仅供参考,这段代码可以正常工作,因为我已经对其进行了测试。所以请将此答案标记为正确答案。您可以评论任何进一步的查询。