This project has moved and is read-only. For the latest updates, please go here.

How can I access a UserControl (MediaElement) in the View from the ViewModel

Jul 27, 2011 at 9:06 AM
Edited Jul 27, 2011 at 9:19 AM

Hello all,

I am very new to the MVVM Light Toolkit (and also to WPF). 

I have the following problem, and don't see how to solve it:

In my MainView I place a MediaElement, and also some buttons, which control the behavior of the application. I wired the button with RelayCommands to the ViewModel, so thats already working fine. I figured out, that it is easy to start and stop the MediaElement via a CodeBehind file of the MainView, but I like to control the MediaElement from within the ViewModel via the RelayCommands. So I thought to inject a reference to the MedieElement in to the ViewModel. As I find out the ViewModelLocator is contructed first, and this will contruct the ViewModel and at last the MainView is constructed, so an injection won't work in this case.

So, what would be the best solution to let the ViewModel receive a reference to the MediaElement? Messages?

best regards


ps: I also asks this on stackflow

Aug 16, 2011 at 8:03 PM

Try using a Content Control in the view. Bind the content to the MediaElement in the ViewModel.