将可观察集合绑定到不起作用的树视图

时间:2015-02-13 05:22:51

标签: c# xaml binding treeview observablecollection

我遇到麻烦将可观察的集合绑定到树视图这里是我的代码,似乎是100%准确但不起作用。目的是能够点击专辑或歌曲,然后在相应的歌曲信息/专辑信息选项卡中显示信息。

此刻我甚至无法将相册名称绑定到树视图...更不用说包含歌曲的子组(如果专辑包含任何内容(专辑1)

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="203" Width="524.579">
    <Grid Margin="0,0,0,12">

        <TreeView x:Name="AlbumListTree"
        HorizontalAlignment="Left"
        ItemsSource="{Binding abumList}" >
            <TreeView.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding AlbumName}" />
                </DataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>

        <TabControl HorizontalAlignment="Left" Height="141" Margin="191,10,0,0" VerticalAlignment="Top" Width="186">
            <TabItem Header="Album Info">
                <Grid Background="#FFE5E5E5">
                    <Label x:Name="AlbumName" HorizontalAlignment="Left" Height="33" Margin="10,10,0,0" VerticalAlignment="Top" Width="130"/>
                    <Label x:Name="AlbumRelease" HorizontalAlignment="Left" Height="32" Margin="10,48,0,0" VerticalAlignment="Top" Width="130"/>
                </Grid>
            </TabItem>
            <TabItem Header="Song Info">
                 <Grid Background="#FFE5E5E5">
                    <Label x:Name="name" HorizontalAlignment="Left" Height="33" Margin="10,10,0,0" VerticalAlignment="Top" Width="130"/>
                    <Label x:Name="artist" HorizontalAlignment="Left" Height="32" Margin="10,48,0,0" VerticalAlignment="Top" Width="130"/>
                    <Label x:Name="lenghtinSec" HorizontalAlignment="Left" Height="32" Margin="10,68,0,0" VerticalAlignment="Top" Width="130"/>
                </Grid>
            </TabItem>
        </TabControl>
    </Grid>
</Window>

这是我的歌曲课程

namespace WpfApplication1
{
    public class Song
    {
        public string artist;
        public string name;
        public int lenghtinSec;
        public override string ToString()
        {

            return "" + name;
        }

        public Song(string f, string l, int lis)
        {
            name = l;
            artist = f;
            lenghtinSec = lis;
        }

    }
}

这是我的专辑类

namespace WpfApplication1
{
    public class Album 
    {
        public string AlbumName;
        public string AlbumRelease;
        ObservableCollection<Song> songsAlbum;

        public override string ToString()
        {

            return AlbumName;
        }

        public Album(String a, String b, ObservableCollection<Song> c)
        {
            AlbumName = a;
            AlbumRelease = b;
            songsAlbum = c;
        }


        public static ObservableCollection<Album> GetAlbumList()
        {
            return new ObservableCollection<Album>()
            {
                new Album("Get Rich ","Sept 4", new ObservableCollection<Song>{new Song("50 Cent ","In Da Club",155),
                                                                                new Song("Eminem","Without Me",332),
                                                                                new Song("Meek Mill","Dreams & Nightmares",213)}),
                new Album("Shady Records","Sept 14",new ObservableCollection<Song>{})

            };
        }

    }
}

最后是我的C#

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += MainWindow_Loaded;
        }

        public ObservableCollection<Album> albumList { get; set; }



        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {

            albumList = Album.GetAlbumList();
            AlbumListTree.DataContext = albumList;

        }

        private void AlbumListTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            Album selectedAlbum = (Album)AlbumList.SelectedItem;
            AlbumName.Content = selectedAlbum.AlbumName;
            AlbumRelease.Content = selectedAlbum.AlbumRelease;
        }

    }



}

0 个答案:

没有答案