簡體   English   中英

通過綁定創建TreeView(子節點作為列表) <MyType> )在MVVM中

[英]Create TreeView from binding (child node as List<MyType>) in MVVM

我需要TreeView來顯示播放列表及其歌曲,它看起來應該像這樣:

-Playlist1
--Song1
--Song2
--Song3
-Playlist2
--Song1
--Song2
--Song3
--Song4
-Playlist3
--Song1
--Song2

TreeView可以有無限數量的播放列表,每個節點具有不同數量的歌曲。 我想使用MVVM實現此目的。

MainWindow.xaml

<TreeView ItemsSource="{Binding PlayLists}">
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type types:Playlist}" ItemsSource="{Binding PlayLists}">
            <TextBlock Text="{Binding Path=Name}"/>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate DataType="{x:Type types:Song}">
            <TextBlock Text="{Binding Path=Title}"/>
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>

播放清單

public class Playlist
{
    public string Name { get; set; }
    public List<Song> Songs { get; set; }

    public static ObservableCollection<Playlist> Playlists { get; set; } = new ObservableCollection<Playlist>();
}

Song.cs

public class Song
{
    public string FullPath { get; set; }
    public string Title { get; set; }
    public string Artist { get; set; }
    public string Genre { get; set; }
    public string Length { get; set; }
    public string Size { get; set; }
}

MainViewModel.cs

public ObservableCollection<Playlist> PlayLists { get; set; }

public MainViewModel()
{

    /* Get Play-lists */
    string listsPath = Path.Combine(
        Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "AppName",  "Playlists");
    if (!Directory.Exists(listsPath))
    {
        Directory.CreateDirectory(listsPath);
    }

    string[] playlists = Directory.GetDirectories(listsPath);

    foreach (var playlist in playlists)
    {
        var songs = new List<Song>();
        string[] songsInPath = Directory.GetFiles(playlist);
        foreach (var song in songsInPath)
        {
            songs.Add(NewSong(song));
        }

        Playlist newPlaylist = new Playlist()
        {
            Name = playlist.Split(Path.DirectorySeparatorChar).Last(),
            Songs = songs
        };

        Playlist.Playlists.Add(newPlaylist);
    }
    PlayLists = Playlist.Playlists;
}

看起來是這樣的:

在此處輸入圖片說明

播放列表中的每首歌曲都應該有一個子節點。

TreeView僅顯示PlaylistName ,而不顯示SongTitle 我意識到這是因為Playlist的屬性Songs實際上是List<Song> 我不確定如何從屬性Playlist.Songs顯示每首Song的標題。

有人可以幫我嗎

您的Songs結構錯誤

看一下這個例子:

<HierarchicalDataTemplate DataType="{x:Type self:Family}" ItemsSource="{Binding Members}">
    <StackPanel Orientation="Horizontal">
        <Image Source="/WpfTutorialSamples;component/Images/group.png" Margin="0,0,5,0" />
        <TextBlock Text="{Binding Name}" />
        <TextBlock Text=" [" Foreground="Blue" />
        <TextBlock Text="{Binding Members.Count}" Foreground="Blue" />
        <TextBlock Text="]" Foreground="Blue" />
    </StackPanel>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type self:FamilyMember}">
    <StackPanel Orientation="Horizontal">
        <Image Source="/WpfTutorialSamples;component/Images/user.png" Margin="0,0,5,0" />
        <TextBlock Text="{Binding Name}" />
        <TextBlock Text=" (" Foreground="Green" />
        <TextBlock Text="{Binding Age}" Foreground="Green" />
        <TextBlock Text=" years)" Foreground="Green" />
    </StackPanel>
</DataTemplate>

給定此模型

public class Family
{
    public Family()
    {
        this.Members = new ObservableCollection<FamilyMember>();
    }

    public string Name { get; set; }

    public ObservableCollection<FamilyMember> Members { get; set; }
}

public class FamilyMember
{
    public string Name { get; set; }

    public int Age { get; set; }
}

看一下這個作為參考:

TreeView,數據綁定和多個模板

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM