Wilson
Wilson

Reputation: 8768

Binding an Observable Collection to a ListView

XAML:

<Window x:Class="Genesis.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="725" Width="918" Loaded="Window_Loaded"
    DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
    <ListView Margin="22,39,0,0" Name="Library" DataContext="{Binding}" ItemsSource="{Binding _songData}" HorizontalAlignment="Left" Width="854" Height="427" VerticalAlignment="Top">
        <ListView.View>
            <GridView x:Name="gvLibrary" >
                <GridViewColumn Width="220" Header="Title" DisplayMemberBinding="{Binding Title}" x:Name="gvColumnTitle" />
                <GridViewColumn Width="180" Header="Artist" DisplayMemberBinding="{Binding Artist}" x:Name="gvColumnArtist" />
                <GridViewColumn Width="180" Header="Album" DisplayMemberBinding="{Binding Album}" x:Name="gvColumnAlbum" />
                <GridViewColumn Width="180" Header="Location" DisplayMemberBinding="{Binding Location}" x:Name="gvColumnLocation" />
            </GridView>
        </ListView.View>
    </ListView>
</Grid>
</Window>

C#:

ObservableCollection<songInfo> songData = new ObservableCollection<songInfo>();

    public ObservableCollection<songInfo> _songData
    { 
        get 
        { 
            return songData; 
        } 
    }
public ObservableCollection<songInfo> getStoredData()
    {

        string[] songs = System.IO.File.ReadAllLines("library");

        ObservableCollection<songInfo> songs_formatted = new ObservableCollection<songInfo>();

        foreach (string song in songs)
        {
            string[] data = song.Split('|');
            songs_formatted.Add(new songInfo
            {
                Title = data[0],
                Artist = data[1],
                Album = data[2],
                Location = data[3]
            });
        }
        return songs_formatted;


    }
private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        if (System.IO.File.Exists("library"))
        {
            songData = getStoredData();
        }
        else
        {
            F.MessageBox.Show("Could not get your Library.");
        }

        F.MessageBox.Show(songData.ToArray().Length.ToString());
    }
public class songInfo
    {
        public string Title { get; set; }
        public string Artist { get; set; }
        public string Album { get; set; }
        public string Location { get; set; }
    }

The ObservableCollection is being populated correctly at runtime and all elements are correctly formatted: the problem has something to do with binding the collection to the listview and columns.

Upvotes: 0

Views: 1526

Answers (1)

sa_ddam213
sa_ddam213

Reputation: 43596

The problem is you are setting songData but the ListView is bound to _songData the ListView is not going to know you have changed songData.

Try adding a setter to _songData and setting getStoredData(); to that.

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        if (System.IO.File.Exists("library"))
        {
            _songData = getStoredData();
        }
    }

    private ObservableCollection<songInfo> songData = new ObservableCollection<songInfo>();
    public ObservableCollection<songInfo> _songData
    {
        get { return songData; }
        set { songData = value; }
    }

Upvotes: 1

Related Questions