4

I have a command in a view model called Add and it currently takes in one parameter called Result. I now need more data passed into the command and that is the IsToggled property value of the Switch control.

So if I have the following class:

public class ResultData
{
    public string Result { get; set; }
    public bool IsToggled { get; set; }
}

And a snippet of the XAML in question:

 <Switch IsToggled="false" ThumbColor="Black" OnColor="LimeGreen" HorizontalOptions="End" VerticalOptions="Center" >
    <Switch.Behaviors>
         <behaviours:EventToCommandBehavior EventName="Toggled"                                                            
           Command="{Binding BindingContext.Add, Source={x:Reference 
              MyPageContent}}" 
           CommandParameter="{Binding Result}" />
    </Switch.Behaviors>
 </Switch>

What is the XAML syntax to pass Result & IsToggled using the CommandParameter? I am open to other approaches if you feel that this not the right way to do it.

0

2 Answers 2

5

You can do it just set your switch BindingContext to Result and pass your switch to CommandParameter as parameter and from that parameter you can get any property of switch.

<Switch IsToggled="false" ThumbColor="Black" OnColor="LimeGreen" HorizontalOptions="End" VerticalOptions="Center" BindingContext="{Binding Result}" x:Name="switch">
    <Switch.Behaviors>
         <behaviours:EventToCommandBehavior EventName="Toggled"                                                            
           Command="{Binding BindingContext.Add, Source={x:Reference 
              MyPageContent}}" 
           CommandParameter="{Binding Source={x:Reference switch}}" />
    </Switch.Behaviors>
</Switch>
Sign up to request clarification or add additional context in comments.

Comments

1

How to pass custom object via XAML? What is the XAML syntax to pass Result & IsToggled using the CommandParameter?

The CommandParameter is type of object, we could pass a class object value as the parameter. To pass both the Result & IsToggled, try to set a ResultData object as the CommandParameter and get the two properties in code behind.

I create a basic demo to test the function, you could refer to the code.

Page.xaml

<Label Text="Welcome to Xamarin.Forms!"
       HorizontalOptions="CenterAndExpand">
    <Label.GestureRecognizers>
        <TapGestureRecognizer Command="{Binding TappedCommand}" CommandParameter="{Binding _Parameter}"/>
    </Label.GestureRecognizers>
</Label>

Page.xaml.cs

public partial class Page4 : ContentPage
{
    public Page4()
    {
        InitializeComponent();

        BindingContext = new Model_4();
    }
}

Model class

public class Model_4
{
    public TheParameter _Parameter { get; set; }

    public ICommand TappedCommand { private set; get; }

    public Model_4()
    {
        TappedCommand = new Command(OnTapped);
        _Parameter = new TheParameter { Property_1 = "Property_1", Property_2 = "Property_2" };
    }

    private void OnTapped(object obj)
    {
        var theParameter = obj as TheParameter;
        Console.WriteLine(theParameter.Property_1);
        Console.WriteLine(theParameter.Property_2);
    }
}

public class TheParameter
{
    public string Property_1 { get; set; }
    public string Property_2 { get; set; }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.