Specify the datacontext in EventToCommand

Feb 26, 2010 at 10:54 PM
Edited Feb 27, 2010 at 12:41 AM

I have a UserControl (a part) that has a ViewModel. That UserControl is embedded in another UserControl (a page) with its own ViewModel. When attaching an EventToCommand behavior to the "part" UserControl, the command is sent to the "part" ViewModel because that is the datacontext for that control. I want the command to go to the "page" ViewModel. How can I tell the EventToCommand behavior to use the "page" datacontext instead of the "part" datacontext?

The "page" datacontext is set using a ViewModelLocator.

<UserControl.DataContext>
        <Binding Path="MyVM" Source="{StaticResource Locator}"/>
</UserControl.DataContext>

I have only been able to come up with an unsatisfactory solution and am looking for help solving this problem.

Feb 28, 2010 at 10:07 PM

Give the layout panel, but not the page UserControl, a name. Then see the example below.

<UserControl.DataContext>
        <Binding Path="MyVM" Source="{StaticResource Locator}"/>
</UserControl.DataContext>
    
<Grid x:Name="LayoutRoot">

<local:PartCtrl x:Name="MyPartCtrl"  >
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="MouseEnter">
                    <cmd:EventToCommand  Command='{Binding ElementName="LayoutRoot", Path="DataContext.MyCommand", Mode=OneWay }'  />
                </i:EventTrigger>
            </i:Interaction.Triggers>
</local:PartCtrl>

</Grid>

 

Coordinator
Feb 28, 2010 at 10:39 PM

Hi Orrin,

Sorry for the delay. I just returned from two weeks of absence.

What you mention will work indeed. If your main view model is a singleton (i.e. defined as a static property of the ViewModelLocator), another way is to do:

Command="{Binding Source={StaticResource Locator}, Path=MyVM.MyCommand}"

Cheers and thanks for the feedback!

Laurent

Mar 1, 2010 at 12:25 AM
Laurent,

Thank you for the advice. And thank you for creating a great framework.

Orrin





lbugnion wrote:

From: lbugnion

Hi Orrin,

Sorry for the delay. I just returned from two weeks of absence.

What you mention will work indeed. If your main view model is a singleton (i.e. defined as a static property of the ViewModelLocator), another way is to do:

Command="{Binding Source={StaticResource Locator}, Path=MyVM.MyCommand}"

Cheers and thanks for the feedback!

Laurent

Coordinator
Apr 18, 2010 at 6:58 PM

Hi,

As you are a previous user of the discussion tab on the MVVM Light Codeplex site, I would like to inform you that I decided to encourage the usage of StackOverflow for questions regarding the MVVM Light toolkit. Please tag your questions with the mvvm-light tag.

StackOverflow is an awesome site where tons of developers help others with their technical question.

http://stackoverflow.com/questions/tagged/mvvm-light

I will monitor this tag on the StackOverflow website and do my best to answer questions. The advantage of StackOverflow over the Codeplex discussion is the sheer number of qualified developers able to help you with your questions, the visibility of the question itself, and the whole StackOverflow infrastructure (reputation, up- or down-vote, comments, etc)

Thanks!

Laurent