0
votes

I have a TreeView (I almost copied the same code from XAML Controls Gallary):

    <TreeView
        x:Name="LocalFolderTreeView"
        ItemTemplateSelector="{StaticResource FolderTreeTemplateSelector}"
        ItemsSource="{x:Bind Tree, Mode=OneWay}"
        Visibility="Collapsed" />

And the XAML of ItemTemplateSelector:

    <DataTemplate x:Key="FolderTemplate" x:DataType="data:FolderTree">
        <TreeViewItem
            DoubleTapped="FolderTreeItem_DoubleTapped"
            IsDoubleTapEnabled="True"
            IsExpanded="False"
            ItemsSource="{x:Bind Files}">
            <StackPanel Orientation="Horizontal">
                <StackPanel.ContextFlyout>
                    <MenuFlyout Opening="OpenPlaylistFlyout" />
                </StackPanel.ContextFlyout>
                <SymbolIcon Symbol="Folder" />
                <TextBlock Margin="0,0,10,0" />
                <TextBlock Text="{x:Bind Path}" />
            </StackPanel>
        </TreeViewItem>
    </DataTemplate>

    <DataTemplate x:Key="FileTemplate" x:DataType="data:Music">
        <TreeViewItem>
            <StackPanel
                DoubleTapped="FileItem_DoubleTapped"
                IsDoubleTapEnabled="True"
                Orientation="Horizontal">
                <StackPanel.ContextFlyout>
                    <MenuFlyout Opening="OpenMusicFlyout" />
                </StackPanel.ContextFlyout>
                <Image Width="20" Source="Assets/colorful_no_bg.png" />
                <TextBlock Margin="0,0,10,0" />
                <TextBlock Text="{x:Bind Name}" />
            </StackPanel>
        </TreeViewItem>
    </DataTemplate>

    <templateselector:FolderTreeTemplateSelector
        x:Key="FolderTreeTemplateSelector"
        FileTemplate="{StaticResource FileTemplate}"
        FolderTemplate="{StaticResource FolderTemplate}" />

C# of ItemTemplateSelector is

public class FolderTreeTemplateSelector : DataTemplateSelector
{
    public DataTemplate FolderTemplate { get; set; }
    public DataTemplate FileTemplate { get; set; }

    protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
    {
        if (item is Models.FolderTree) return FolderTemplate;
        if (item is Models.Music) return FileTemplate;
        return null;
    }
}

It is used here

My ViewModels are defined here.

When I add this to the TreeView, my app crashes. Tree is not null because I also defined GridView that allows me to see it.

        ItemsSource="{x:Bind Tree, Mode=OneWay}"

What is wrong?

1
I checked LocalFoldersPage, I can't find you have set the itemsource for the TreeView. So, I'm not sure the data structure is matched with your xaml.Nico Zhu - MSFT
@NicoZhu-MSFT I removed it because it caused my app to crash. You can add it back.Seaky Lone
How can I navigate LocalFoldersPage ?Nico Zhu - MSFT
@NicoZhu-MSFT You need a folder that contains a folder of music files. Basically set folder1 to be your root path if you have music file like folder1/folder2/music.mp3.Seaky Lone
I check the FolderTree it's not matched actual folder structure.Nico Zhu - MSFT

1 Answers

1
votes

The default ItemsSource is collection type, but in above code the Tree data source is FolderTree, it will cause argument error, please modify it as collection base on your actual data structure.