0

I am having troubles binding an observable collection to a treeview here is my code, seems to be 100% accurate but doesnt work. the purpose is to be able to click an album or a song and then show the info in the corresponding songinfo/album info tab.

at the moment i cannot even get just the album name to bind to the treeview.. let alone a sub group containing songs if the album contains any ( album 1 does )

<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>

here is my Song Class

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;
        }

    }
}

here is my album class

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>{})

            };
        }

    }
}

and finally my 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;
        }

    }



}
mathERSI
  • 66
  • 4
  • possible duplicate of [WPF TreeView HierarchicalDataTemplate - binding to object with multiple child collections](http://stackoverflow.com/questions/1912481/wpf-treeview-hierarchicaldatatemplate-binding-to-object-with-multiple-child-co) –  Feb 13 '15 at 05:34
  • 'Binding abumList' is missing an 'l'. If you go into debug mode, you'll see such errors in the output pane. – wezten Feb 13 '15 at 08:47

0 Answers0