WPF无法填充树视图

时间:2013-12-04 05:21:48

标签: c# wpf xaml treeview

我是WPF的新手,并试图围绕处理数据的首选方式。我发现这个link解释了树视图的数据绑定。我试图以类似的方式创建我的代码,但我不明白为什么代码运行良好而我的代码没有。

无论如何,我已经为艺术家/专辑/歌曲定义了一些课程

class LibArtist
{
    public string Name { get { return mName; } }
    string mName;
    public ObservableCollection<LibAlbum> Albums;

    public LibArtist(string name)
    {
        mName = name;
        Albums = new ObservableCollection<LibAlbum>();
    }


}

class LibAlbum
{
    public string Name { get { return mName; } }
    public string Artist { get { return mArtist.Name; } }
    public uint Year { get { return mYear; } }
    public ObservableCollection<LibSong> mSongs = new ObservableCollection<LibSong>();
    uint mYear;
    LibArtist mArtist;
    string mName;

    public LibAlbum(string pName, LibArtist pArtist, uint pYear)
    {
        mName = pName;
        mArtist = pArtist;
        mYear = pYear;
    }
}
class LibSong
{
    public string Title { get { return mName; } }
    public string Artist { get { return mArtist; } }
    public string Album { get { return mAlbum; } }
    public string Location { get { return mLocation; } }
    public uint Year { get { return mYear; } }
    string mName;
    uint mYear;
    string mAlbum;
    string mArtist;
    string mLocation;
    public LibSong(string pSongLocation)
    {
        mLocation = pSongLocation;
        TagLib.File lFile = TagLib.File.Create(pSongLocation);
        mAlbum = lFile.Tag.Album;
        mName = lFile.Tag.Title;
        mArtist = lFile.Tag.AlbumArtists.Length > 0 ? lFile.Tag.AlbumArtists[0] : "???";
        //use tag lib to fill the data if this file exists
        mYear = lFile.Tag.Year;
    }

    public override bool Equals(object obj)
    {
        LibSong temp = obj as LibSong;
        if (temp == null)
            return false;
        if (temp.Location == this.Location)
            return true;
        if (temp.Artist == this.Artist && temp.Album == this.Album && temp.Year == this.Year)
            return true;
        return false;
    }
}

这些都坐在图书馆课上:

class Library
{
    public SortedDictionary<string, List<string>> mArtistsToAlbums;
    SortedDictionary<string, List<LibSong>> mAlbumsToSongs;
    public List<LibSong> mSongList;
    public ObservableCollection<LibSong> mSongList2;
    public ObservableCollection<LibAlbum> mAlbumList;

    public ObservableCollection<LibArtist> mArtistList;
    ...
}

在我的主窗口中,我将树视图的数据上下文设置为库对象:

public MainWindow()
    {
        mPlayer = new izPlayer(0);


        InitializeComponent();

        libraryTreeView.DataContext = mLibrary;
        mLibrary = new Library();
        mLibrary.CreateTestData();

在我的xaml中,我像这样定义树视图:

<TreeView Name="libraryTreeView"
        HorizontalAlignment="Left"
        ItemsSource="{Binding mArtistList}"
        Height="443" Margin="10,50,0,0" VerticalAlignment="Top" Width="344" MouseDoubleClick="libraryTreeView_MouseDoubleClick" 
         >
        <TreeView.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}" />
            </DataTemplate>
        </TreeView.ItemTemplate>      
    </TreeView>

当我运行它时,我在树视图中没有显示任何内容。正如我所说,我不确定为什么这与示例代码不同,或者为什么它不在mArtistList中显示数据。

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:1)

特别是TreeView丹尼斯的答案是一个很好的资源。如果您没有获得任何项目,即使在最高级别的想法,也可能是由于无效的绑定源。看起来Library声明了公共字段

public ObservableCollection<LibArtist> mArtistList;

为了在XAML中使用绑定,这些源需要是公共属性

public ObservableCollection<LibArtist> mArtistList { get; set; }

答案 1 :(得分:0)

这与示例代码(我的意思是XAML差异)完全不同。

WPF中数据绑定TreeView的主要概念是您必须为节点描述hierarchical data templates,因为您希望显示分层数据。

您的XAML应如下所示:

<TreeView.Resources>
    <HierarchicalDataTemplate DataType="{x:Type yourNamespace:LibArtist}" ItemsSource="{Binding Albums}">
    <!-- the template tree for displaying artist's data -->
    </HierarchicalDataTemplate>

    <HierarchicalDataTemplate DataType="{x:Type yourNamespace:LibAlbum}" ItemsSource="{Binding Songs}">
    <!-- the template tree for displaying song's data -->
    </HierarchicalDataTemplate>

    <!-- and so on -->
</TreeView.Resources>