I have created a class Track which represents a song in the playlist:
public class Track
{
public Uri Path
{
get { return path; }
set { path = value; }
}
public TrackState State
{
get { return state; }
set { state = value; }
}
private Uri path;
private TrackState state;
}
Next I have created MainWindowController class which interacts between the UI window and the Track class:
public class MainWindowController : INotifyPropertyChanged
{
public ObservableCollection<Track> Playlist
{
get { return playlist; }
set
{
if (value != this.playlist)
{
playlist = value;
NotifyPropertyChanged("Playlist");
}
}
}
public int NowPlayingTrackIndex
{
set
{
if (value >= 0)
{
playlist[nowPlayingTrackIndex].State = TrackState.Played;
playlist[value].State = TrackState.NowPlaying;
this.nowPlayingTrackIndex = value;
}
}
}
private ObservableCollection<Track> playlist;
private int nowPlayingTrackIndex;
}
Basically, this class stores playlist collection and an index of the currently played track. Lastly, I have created the UI window in WPF:
<Window ...>
...
<ListBox
Name="PlaylistListBox"
ItemsSource="{Binding Source={StaticResource ResourceKey=PlaylistViewSource}}"
ItemTemplateSelector="{Binding Source={StaticResource ResourceKey=TrackTemplateSelector}}"
MouseDoubleClick="PlaylistListBox_MouseDoubleClick" />
...
</Window>
and the corresponding code behind:
...
private void PlaylistListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
int index = this.PlaylistListBox.SelectedIndex;
this.windowController.NowPlayingTrackIndex = index;
}
...
Items source points to the static resource where the CollectionViewSource
is defined. The ItemTemplateSelector
defines which DataTemplate
to use for list box items depending on the track state (NowPlaying or Played).
When the user double-clicks the playlist item, the NowPlayingTrackIndex
in the MainWindowController
gets updated and it updates the track state. The problem is, the DataTemplates
for list box items do not get updated on the window, i.e. the the double-clicked list box item does not change the data template. Why?
I tried setting the PropertyChanged
for track state but it didn't help. What am I missing? Thank you.