user2847238
user2847238

Reputation: 169

Add member to bound list in WPF

I am practicing data binding in WPF.

I have Song.cs file with Song Class. Values don't change, so there is no point of adding INotifyPropertyChanged.

class Song
{
    private string title;
    public string Title
    {
        get { return title; }
        set { title = value; }
    }

    private string artist;
    public string Artist 
    {
        get { return artist; }
        set { artist = value; }
    }

    private string album;
    public string Album
    {
        get { return album; }
        set { album = value; }
    }

    public static Song Load(string record)
    {
        var split = record.Split(';');
        Song song = new Song();
        song.Artist = split[0];
        song.Album = split[1];
        song.Title = split[2];
        return song;
    }

    public override string ToString()
    {
        return String.Format("{0} / {1} / {2}", artist, album, title);
    }
}

Then I have SongList.cs which contains method to load some initial songs and this class derives from ObservableColection.

class SongList : ObservableCollection<Song>
{
    public static SongList Load()
    {
        SongList result = new SongList();

        Song s1 = Song.Load("The Offspring;The Americana;Staring At The Sun");
        result.Add(s1);

        Song s2 = Song.Load("Metallica;Reload;Unforgiven II");
        result.Add(s2);

        return result;
    }
}

The data is bound to Listbox in XAML

 <ListBox Name="listBox" ItemsSource="{Binding}" />

And then in MainWindow Constructor I am setting datacontext to load list of songs

public MainWindow()
{
    InitializeComponent();
    SongList songs = SongList.Load();
    Song newSong = Song.Load("AC/DC;Back In Black;Hells Bells");
    songs.Add(newSong);
    listBox.DataContext = songs;
}

And here is my question. I want to add Song to my SongList everytime I click the button, but I have no idea how to acces this list from my ButtonClick event handler.

private void Button1_Click(object sender, RoutedEventArgs e)
{
    //?
}

Upvotes: 0

Views: 163

Answers (2)

Alberto Solano
Alberto Solano

Reputation: 8227

Since you're practicing with WPF, I think you should start implementing your applications, applying the MVVM pattern. You can find some useful resources in this question.

Anyway, to keep things simple you should have something like this:

public class MainWindow
{
SongsList songs;

public MainWindow()
{
    InitializeComponent();
    songs = new SongList();
    listBox.DataContext = songs;
}

public void Button_Click(object sender, EventArgs e)
{
    songs = SongList.Load();
    Song newSong = Song.Load("AC/DC;Back In Black;Hells Bells");
    songs.Add(newSong);
    listbox.Items.Refresh();
}

}

Applying the MVVM pattern, creating a related ViewModel for your application and exposing the songs list as property of ViewModel bound to the ItemsSource property of your ListBox, you won't need to invoke listbox.Items.Refresh(); because you will implement the INotifyPropertyChanged interface in your ViewModel.

Upvotes: 2

Carbine
Carbine

Reputation: 7903

When you say Song newSong = Song.Load("AC/DC;Back In Black;Hells Bells");

You are making newSong a local variable. You need to have it as a class level member variable to make it accessible. So declare it outside MainWindow then modify it in Button_Click.

I hope you have kept both of them in the code behind of the same XAML File.

Upvotes: 1

Related Questions