Bind from 2 ViewModels in one XAML element

喜欢而已 提交于 2021-01-29 16:31:37

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!