问题
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 ?
回答1:
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>
来源:https://stackoverflow.com/questions/59271520/bind-from-2-viewmodels-in-one-xaml-element