Setting the Xamarin.Forms Binding in Code

I am trying to set a binding in TapGestureRecognizerin code, and I cannot figure out how to do this. The working xaml looks something like this:

<Grid>
    <Grid.GestureRecognizers>
        <TapGestureRecognizer Command="{Binding LaunchLocationDetailsCommand}" CommandParameter="{Binding}" />
    </Grid.GestureRecognizers>
</Grid>

And in C # it looks something like this:

var gridTap = new TapGestureRecognizer();
gridTap.SetBinding(TapGestureRecognizer.CommandProperty, new Binding("LaunchLocationDetailsCommand"));
gridTap.SetBinding(TapGestureRecognizer.CommandParameterProperty, new Binding(/* here where I'm confused */));

var grid = new Grid();
grid.GestureRecognizers.Add(gridTap);

My confusion is about binding CommandParameterProperty. In xaml, it is simply {Binding}without another parameter. How is this done in code? Transfer in new Binding()or this.BindingContextnot working.

+4
source share
2 answers

Linking CommandPropertyis the same as what you did.

, CommandParameterProperty Binding, .

, Source, .

, BindingContext, , Null, , , , .

Grid BindingContext (objGrid.BindingContext = objMyView2).

, Source (Source = objGrid.BindingContext).

, Output, .

        MyView2 objMyView2 = new MyView2();
        objMyView2.SomeProperty1 = "value1";
        objMyView2.SomeProperty2 = "value2";

        objMyView2.LaunchLocationDetailsCommand_WithParameters = new Command<object>((o2)=>
        {
            LaunchingCommands.LaunchLocationDetailsCommand_WithParameters(o2);
        });

        Grid objGrid = new Grid();
        objGrid.BindingContext = objMyView2;
        objGrid.HeightRequest = 200; 
        objGrid.BackgroundColor = Color.Red;

        TapGestureRecognizer objTapGestureRecognizer = new TapGestureRecognizer();
        objTapGestureRecognizer.SetBinding(TapGestureRecognizer.CommandProperty, new Binding("LaunchLocationDetailsCommand_WithParameters"));

        Binding objBinding1 = new Binding()
        {
            Source = objGrid.BindingContext
        };
        objTapGestureRecognizer.SetBinding(TapGestureRecognizer.CommandParameterProperty, objBinding1);
        //
        objGrid.GestureRecognizers.Add(objTapGestureRecognizer);

: -

MyView2: -

public class MyView2
    : ViewModelBase
{
    public string SomeProperty1 { get; set; }
    public string SomeProperty2 { get; set; }

    public ICommand LaunchLocationDetailsCommand_WithParameters { get; set; }
}

LaunchingCommands: -

public static class LaunchingCommands
{
    public static void LaunchLocationDetailsCommand_WithParameters(object pobjObject)
    {
        System.Diagnostics.Debug.WriteLine("SomeProperty1 = " + (pobjObject as MyView2).SomeProperty1);
    }
}

ViewModelBase: -

public abstract class ViewModelBase
    : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public void RaisePropertyChanged(string pstrPropertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(pstrPropertyName));
        }
    }

}
+2

{Binding}, , . , .

Binding, .

var binding = new Xamarin.Forms.Binding() { Source = this.BindingContext };
0

Source: https://habr.com/ru/post/1627539/


All Articles