Search code examples
c#xamlmvvmuwprelaycommand

Is it possible to have two parameters with CommandParameter and RelayCommand in UWP?


I want to do this:

XAML

<Button Content="Remove item" Command="{Binding ElementName=lvBackpacks, Path=DataContext.RemoveItemFromBackpackCommand}" CommandParameter="{Binding property1} {Binding property2}}"/>

ViewModel

RemoveItemFromBackpackCommand = new RelayCommand<Type1, Type2>(param1, param2 =>
        {
            DoSomething(param1, param2);
        });

PS: This is just pseudocode to visualize. Its is obviously not working.

Is there some way I can pass two commandparameters?


Solution

  • No, you can't bind 2 separate objects to the same CommandParameter property.

    However, you can create an object containing all the parameters that you need for your command and bind that instead.

    Example:

    Your RemoveItem command requires both a Backpack and a SchoolGear.

    Create an object like the following,

    public class SchoolGearItemViewModel
    {
        Backpack Backpack { get; set; }
        SchoolGear SchoolGear { get; set; }
    }
    

    and change your command so that it takes a SchoolGearItemViewModel parameter instead of the other 2 objects.