How can I access a UserControl (MediaElement) in the View from the ViewModel (MVVM Light Toolkit)
I am very new to the MVVM Light Too开发者_如何学Pythonlkit (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
Bernhard
ps: I also asked this question at the discussion panel at mvvm light toolkit homepage (codeplex)
So I thought to inject a reference to the MedieElement in to the ViewModel
Don't do that. In MVVM you should never have a reference to a UI element in the ViewModel. Instead, raise events from the ViewModel that are handled in the code-behind to control the MediaElement
精彩评论