Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass the Button as CommandParameter from XAML in a Xamarin.Forms Page?

I would like to pass a Xamarin.Forms.Button in it's own Command as the CommandParameter to my ViewModel. I know how to achieve this from the code behind e.g. ...

XAML (with most properties missed out for brevity)

<Button x:Name="myButton"
    Text="My Button"
    Command="{Binding ButtonClickCommand}"/>

XAML.cs

public partial class MyTestPage
{
    public MyTestPage()
    {
        InitializeComponent();

        myButton.CommandParameter = myButton;
    }
}

ViewModel

public class MyViewModel : ViewModelBase
{
    public MyViewModel()
    {
        ButtonClickCommand = new Command(
            (parameter) =>
            {
                var view = parameter as Xamarin.Forms.Button;
                if (view != null)
                {
                    // Do Stuff
                }
            });
    }

    public ICommand ButtonClickCommand { get; private set; }
}

... BUT is it possible to declare the CommandParameter in the XAML itself? Or in other words what is the binding syntax to set the parameter to the button itself?

<Button x:Name="myButton"
        Text="My Button"
        Command="{Binding ButtonClickCommand}"
        CommandParameter="{[WHAT WOULD GO HERE]}"/>

btw I've already tried CommandParameter="{Binding RelativeSource={RelativeSource Self}}" and that didn't work.

Thanks,

like image 706
Gavin Sutherland Avatar asked Sep 18 '14 12:09

Gavin Sutherland


People also ask

What is ICommand in Xamarin Forms?

Using a Windows class that implements ICommand allows you to share your ViewModels between Windows applications and Xamarin. Forms applications. If sharing ViewModels between Windows and Xamarin. Forms is not a concern, then you can use the Command or Command<T> class included in Xamarin.

How do you call ICommand from code?

You can call the ICommand. Execute() method. Here is a small example from a project I have.... You can call the command with code like this....

What is command parameter WPF?

CommandParameter - represents a user-defined data value that can be passed to the command when it is executed.

What is XAML in xamarin forms?

The eXtensible Application Markup Language (XAML) is an XML-based language created by Microsoft as an alternative to programming code for instantiating and initializing objects, and organizing those objects in parent-child hierarchies.


2 Answers

Xamarin.Forms has a Reference markup extension that does just that:

<Button x:Name="myButton"
    Text="My Button"
    Command="{Binding ButtonClickCommand}"
    CommandParameter="{x:Reference myButton}"/>

Although, this is the first time I'm seeing this need, and you probably can better separate your Views from your ViewModels and solve this by using a cleaner pattern, or by not sharing a command across buttons.

like image 126
Stephane Delcroix Avatar answered Oct 08 '22 11:10

Stephane Delcroix


 <Button x:Name="myButton"
        Text="My Button"
        Command="{Binding ButtonClickCommand}"
        CommandParameter="{x:Reference myButton}"/>

In your ViewModel

public YourViewModel()
{
    ButtonClickCommand= new Command(ButtonClicked);
}

private async void ButtonClicked(object sender)
{
    var view = sender as Xamarin.Forms.Button;
}
like image 29
Luiz Fernando Corrêa Leite Avatar answered Oct 08 '22 11:10

Luiz Fernando Corrêa Leite