Search code examples
c#wpfeventsmvvmmvp

Is there a view container where I can wire a ViewModel event to a View action?


I have a collection of views and I would like to be able to call a method on the view in response to an event raised in the ViewModel.

I am currently using a ItemsControl with a DataTemplate but this does not give me an opportunity to intercept the View and ViewModel and wire them up. Is there a container that is better suited for this?


Solution

  • From:

        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"   
    

    User : Source Object is your ViewModel.

       <UserControl>
            <i:Interaction.Triggers>
                <i:EventTrigger SourceObject="{Binding}" EventName="YourEvent">
                    <ei:CallMethodAction MethodName="YourMethod"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </UserControl>
    

    If you place it in a scope of another element then your view (UserControl) add

     <ei:CallMethodAction MethodName="YourMethod" TargetObject="{Binding ElementName=yourUserControl}"/>