Reputation:
In my code I have listbox:
<ListBox Name="ToPlayList">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<CheckBox Checked="Song_Checked" Unchecked="Song_Unchecked"/>
<TextBlock Text="{Binding SongTitle}" Foreground="White"/>
<TextBlock Text="{Binding SongArtist}" Foreground="White" Margin="0,0,7,0"/>
<TextBlock Text="{Binding SongAlbum}" Foreground="White"/>
<TextBlock Text="{Binding SongDate}" Foreground="White" Margin="7,0,7,0"/>
<TextBlock Text="{Binding SongDuration}" Foreground="White"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
It's filled by:
ToPlayList.ItemsSource = ToPlay;
Where ToPlay
is:
public List<Song> ToPlay = new List<Song>();
Song Model:
public class Song
{
public string SongTitle { get; set; }
public string SongArtist { get; set; }
public string SongDate { get; set; }
public string SongAlbum { get; set; }
public string SongPath { get; set; }
public string SongDuration { get; set; }
public string SongGenre { get; set; }
}
I'm finding function, which make List<Song>
filled by Song
objects, which are checked in listbox. Maybe it's trivial, but I am helpless. Thanks for reply.
Upvotes: 0
Views: 56
Reputation: 445
Add one more property to your song class as
public class Song
{
public bool IsChecked { get; set; }
public string SongTitle { get; set; }
public string SongArtist { get; set; }
public string SongDate { get; set; }
public string SongAlbum { get; set; }
public string SongPath { get; set; }
public string SongDuration { get; set; }
public string SongGenre { get; set; }
}
Change ListBox as
<ListBox Name="ToPlayList">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsChecked}"/>
<TextBlock Text="{Binding SongTitle}" Foreground="Black"/>
<TextBlock Text="{Binding SongArtist}" Foreground="White" Margin="0,0,7,0"/>
<TextBlock Text="{Binding SongAlbum}" Foreground="White"/>
<TextBlock Text="{Binding SongDate}" Foreground="White" Margin="7,0,7,0"/>
<TextBlock Text="{Binding SongDuration}" Foreground="White"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Now on a button click or something
private void Button_Click(object sender, RoutedEventArgs e)
{
var check = ((List<Song>)(ToPlayList.ItemsSource)).Where(x=>x.IsChecked);
}
Upvotes: 1