【问题标题】:Xamarin.Forms View-to-View binding without changing BindingContextXamarin.Forms 视图到视图绑定而不更改 BindingContext
【发布时间】:2015-03-19 21:07:49
【问题描述】:

我在 XAML 中定义了一个 Button,其中包含一个 Command,其中包括一个 CommandParameterCommand 绑定到当前视图的 ViewModel,而 CommandParameter 绑定到当前视图中的另一个控件。在 WPF 中,这看起来像:

<TextBox x:Name="MyTextBox" />
<Button Command="{Binding ViewmodelCommand}" 
        CommandParameter="{Binding ElementName=MyTextBox, Path=Text}" />

根据documentation,在 Xamarin.Forms 视图到视图绑定可以通过使用 x:Reference 标记扩展更改控件的 BindingContext 来实现:

<Button BindingContext="{x:Reference Name=MyTextBox}"
        CommandParameter="{Binding Path=Text}" />

但是,在这种情况下,我无法将 Command 绑定到全局 Viewmodel!这种情况有什么解决方案吗?

【问题讨论】:

    标签: data-binding xamarin.forms elementname


    【解决方案1】:

    试试这个:

    <Button Command="{Binding ViewmodelCommand}" 
            CommandParameter="{Binding Source={x:Reference MyTextBox}, Path=Text}" />
    

    【讨论】:

      【解决方案2】:

      你解决了吗?

      我所做的是添加一个绑定 MyTextBox viewModel 比如:

      MyTextBox.SetBinding(Entry.TextProperty, "PropName");

      btn.SetBinding(Button.CommandProperty, "CommandName"); btn.SetBinding(Button.CommandParameterProperty, "PropName");

      【讨论】:

      • 你说得对,我就是这样解决的。虽然我希望有一个更短的解决方案,但目前看来这是唯一的方法。
      猜你喜欢
      • 2016-08-31
      • 1970-01-01
      • 1970-01-01
      • 2015-12-12
      • 2020-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多