uwp switch datatemplate - 绑定 itemssource

uwp switch data template - rebinding itemsource

我正在制作一个 win 10 通用应用程序,我有两个对象数组:

private ObservableCollection<Song> Songs;
private ObservableCollection<JonPlaylist> Playlists;

和一个列表视图来显示其中的数据:

<Page.Resources>
        <DataTemplate x:DataType="data:Song" x:Key="SongDataTemplate">
            <StackPanel>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Name="ListSongName" Text="{x:Bind Title}" VerticalAlignment="Bottom" HorizontalAlignment="Left"/>
                    <TextBlock Name="ListArtist" Text="{x:Bind Artist}" VerticalAlignment="Bottom" HorizontalAlignment="Left"/>
                </StackPanel>
            </StackPanel>
        </DataTemplate>

    <DataTemplate x:DataType="data:JonPlaylist" x:Key="PlaylistDataTemplate">
        <StackPanel>
            <StackPanel Orientation="Horizontal">
                <TextBlock Name="ListSongName" Text="{x:Bind Title}" VerticalAlignment="Bottom" HorizontalAlignment="Left"/>
            </StackPanel>
        </StackPanel>
    </DataTemplate>

</Page.Resources>

列表视图:

<ListView Name="SongListView" ItemsSource="{x:Bind Songs}" IsItemClickEnabled="True"
                  ItemClick="SongListView_ItemClick" 
ItemTemplate="{StaticResource SongDataTemplate}" />

问题是我希望能够在两个不同数据类型的 arrays/collections 之间切换。

我一直在尝试使用数据模板作为页面资源来完成此操作,并重新绑定 listView.ItemSource 和 listView.ItemTemplate:

private void SwitchViewButton_Click(object sender, RoutedEventArgs e)
{
            SongListView.ItemsSource = Playlists[0].GetType();
            SongListView.ItemTemplate = this.Resources.ElementAt(1);


} 

但是我不知道这是否有效。使用用户控件和可视化状态管理器会更好吗?

与其尝试 rebind/re-template 单个列表视图,不如有两个列表视图控件并根据需要 show/hide 它们?

我非常推荐使用 VisualStateManager 来执行此操作并使用 x:DeferLoadStrategy="Lazy" 来解决性能问题。

如果您需要有关上述任何内容的更多信息,请告诉我。