2

I have a Button defined in XAML that contains a Command including a CommandParameter. The Command binds to the current view's ViewModel, while the CommandParameter binds to another control within the current view. In WPF this would look like:

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

According to documentation, in Xamarin.Forms View-to-View binding is possible via changing a control's BindingContext using the x:Reference markup extension:

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

In this case, however, I'm not able to bind the Command to the global Viewmodel! Are there any solutions to this scenario?

andreask
  • 4,248
  • 1
  • 20
  • 25

2 Answers2

3

Try this:

<Button Command="{Binding ViewmodelCommand}" 
        CommandParameter="{Binding Source={x:Reference MyTextBox}, Path=Text}" />
troYman
  • 1,648
  • 16
  • 18
0

Did you solve this?

what I did was to add a binding MyTextBox <-> viewModel like:

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

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

  • You are right, this is how I solved it. Although I would have liked a shorter solution, it seems this is the only way to do it at the moment. – andreask Mar 19 '15 at 10:46