FetFrumos
FetFrumos

Reputation: 5944

MvxCommand With CommandParameter binding to field

I'm trying using fire MvxCommand with CommandParameter:

   <Mvx.MvxListView
   style="@style/MyList"
   local:MvxBind="ItemsSource Items;"
   local:MvxItemTemplate="@layout/itemfavorite" />

It is my property in ViemModel:

    public List<Data> Items
    {
        get { return _items; }
        set
        {
            _items = value;
            RaisePropertyChanged(() => Items);
        }
    }

It is my model:

public class Data
{
   public int Id{get;set;}
   public string Name {get;set;}
   public ICommand Action {get;set;}
}

It is my command(using in Data):

    public ICommand MyCommand
    {
        get
        {
            return new MvxCommand<int>(async (id) =>
            {
               .....
            }
    }

It is my MvxItemTemplate: ....

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Start"
    android:layout_margin="10dp"
    local:MvxBind="Click Action,CommandParameter=Id"/>

If I use "CommandParameter=Id" - I get unhandle exception. "CommandParameter=1" - it is work. But I need to pass a CommandParameter value field Id. Is it possible?

Upvotes: 2

Views: 1487

Answers (1)

Kiliman
Kiliman

Reputation: 20312

The value for CommandParameter is a static value. Unfortunately you cannot use the name of a property to pass as the command parameter.

You got an exception because Mvx was trying to convert the string "Id" into an int.

Instead of passing Id, you can simply use it in your command handler.

public ICommand MyCommand
{
    get
    {
        return new MvxCommand(async () =>
        {
           // use Id here
        }
    }
}

Upvotes: 3

Related Questions