stubborn
stubborn

Reputation: 310

Bind from 2 ViewModels in one XAML element

I am interested in trying to use information from two ViewModels in one element. Here is my sample. At the beginning of the page I got this

    <ContentPage.BindingContext>
        <vm: MainViewModel />
    </ContentPage.BindingContext> 

... at one point I have an element that I need values from 2 VMs.

<TapGestureRecognizer Command="{Binding CommandValueFromAnotherViewModel}"
                      CommandParameter="{Binding StringValueFromCurrentViewModel}"> // This

    <TapGestureRecognizer.BindingContext>
             <vm:ViewModelBindingWithCommandValue />
    </TapGestureRecognizer.BindingContext>  

</TapGestureRecognizer> 

My binding for the Command property works perfectly, but is there any way to set my CommandParameter value from my "MainViewModel" that contains "StringValueFromCurrentViewModel" property ?

Upvotes: 0

Views: 70

Answers (1)

Leo Zhu
Leo Zhu

Reputation: 14956

you could try this:

<ContentPage.BindingContext>
    <vm:MainViewModel x:Name="root"/>
</ContentPage.BindingContext> 

....


<TapGestureRecognizer Command="{Binding CommandValueFromAnotherViewModel}"
                  CommandParameter="{Binding StringValueFromCurrentViewModel,Source={x:Reference root}}">

<TapGestureRecognizer.BindingContext>
         <vm:ViewModelBindingWithCommandValue />
</TapGestureRecognizer.BindingContext>  

Upvotes: 1

Related Questions