本文介绍了将2个ViewModel绑定到一个XAML元素中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有兴趣尝试在一个元素中使用来自两个ViewModels的信息.这是我的样品.在页面的开头,我得到了这个
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>
...在某一时刻,我有一个需要从2个虚拟机中获取值的元素.
... 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>
我对Command属性的绑定工作正常,但是有什么方法可以从包含"StringValueFromCurrentViewModel"属性的"MainViewModel"中设置CommandParameter值吗?
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 ?
推荐答案
您可以尝试以下方法:
<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>
这篇关于将2个ViewModel绑定到一个XAML元素中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!