Data binding Forms视图可在不更改BindingContext的情况下查看绑定
我在XAML中定义了一个Data binding Forms视图可在不更改BindingContext的情况下查看绑定,data-binding,xamarin.forms,elementname,Data Binding,Xamarin.forms,Elementname,我在XAML中定义了一个按钮,它包含一个命令,包括一个命令参数。命令绑定到当前视图的ViewModel,而命令参数绑定到当前视图中的另一个控件。在WPF中,这看起来像: <TextBox x:Name="MyTextBox" /> <Button Command="{Binding ViewmodelCommand}" CommandParameter="{Binding ElementName=MyTextBox, Path=Text}" /> 但是
按钮
,它包含一个命令
,包括一个命令参数
。命令
绑定到当前视图的ViewModel,而命令参数
绑定到当前视图中的另一个控件。在WPF中,这看起来像:
<TextBox x:Name="MyTextBox" />
<Button Command="{Binding ViewmodelCommand}"
CommandParameter="{Binding ElementName=MyTextBox, Path=Text}" />
但是,在本例中,我无法将命令
绑定到全局Viewmodel!这个场景有什么解决方案吗?你解决了吗
我所做的是添加一个绑定MyTextBox viewModel,如:
MyTextBox.SetBinding(Entry.TextProperty,“PropName”)
btn.SetBinding(Button.CommandProperty,“CommandName”);
btn.设置绑定(Button.CommandParameterProperty,“PropName”) 试试这个:
<Button Command="{Binding ViewmodelCommand}"
CommandParameter="{Binding Source={x:Reference MyTextBox}, Path=Text}" />
你说得对,我就是这样解决的。虽然我希望有一个较短的解决方案,但这似乎是目前唯一的解决方法。
<Button Command="{Binding ViewmodelCommand}"
CommandParameter="{Binding Source={x:Reference MyTextBox}, Path=Text}" />