Search code examples
c#wpfmvvmmediaelement

How do I move to the next song using a MVVM mediaelement


I'm learning C#/WPF/MVVM by building a simple media player. I am using ICollectionView in the MainViewModel for the playlist. I built a separate usercontrol for the mediaelement with some code-behind to handle the volume, position, timeline (http://www.codeproject.com/KB/smart/RSSVideoReader.aspx). I know one of the goals of MVVM is to eliminate code-behind, but the WPF mediaelement is unique. I am now stuck trying to figure how to get the code-behind mediaelement's MediaEnded event to tell the MainViewModel's ICollectionView to MoveCurrentToNext.

This code fails:

MainViewModel

public void GoNext()
    {
        this.collectionView.MoveCurrentToNext();
    }

MyMediaElement.xaml.cs (Code behind)

private void mPlayDefault_MediaEnded(object sender, RoutedEventArgs e)
    {

        mPlayDefault.Stop();
        this.GoNext();            

    }

How do I get the MainViewModel to react to MyMediaElement's event? Thank You


Solution

  • Daniel Pratt was correct (See Comments). Using Interaction.Triggers worked as I had hoped. I ran into a problem with the expressions dll and was totally frustrated. Downloading the dll directly from codeplex and unblocking it solved the problem. Many thanks to Daniel and Stackoverflow.