【问题标题】:WPF can't populate treeviewWPF 无法填充树视图
【发布时间】:2013-12-04 05:21:48
【问题描述】:

我是 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 中的数据。

任何帮助将不胜感激!

【问题讨论】:

    标签: c# wpf xaml treeview


    【解决方案1】:

    专门针对TreeView Dennis 的回答是一个很好的资源。如果您即使在最高级别的想法中也没有获得任何项目,则可能是由于绑定源无效。看起来Library 正在声明公共字段

    public ObservableCollection<LibArtist> mArtistList;
    

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

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

    【讨论】:

    • 成功了,尽管我也开始收到一个错误,即即使我没有设置项目,项目也必须为空才能使用 itemSource。 Clear() 虽然成功了,但一切似乎都在工作。谢谢!
    【解决方案2】:

    这与示例代码完全不同(我的意思是 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>
    

    【讨论】:

    • 对不起,那是我的错。在我开始弄乱文件以尝试找出问题所在后,我尝试查看是否仅显示非层次结构列表。
    猜你喜欢
    • 2013-07-10
    • 1970-01-01
    • 1970-01-01
    • 2013-09-21
    • 2014-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多