c# - Setting Xamarin.Forms binding in code behind -
i'm trying set binding on tapgesturerecognizer
in code , can't figure out right way it. working xaml looks this...
<grid> <grid.gesturerecognizers> <tapgesturerecognizer command="{binding launchlocationdetailscommand}" commandparameter="{binding}" /> </grid.gesturerecognizers> </grid>
and in c#, looks this...
var gridtap = new tapgesturerecognizer(); gridtap.setbinding(tapgesturerecognizer.commandproperty, new binding("launchlocationdetailscommand")); gridtap.setbinding(tapgesturerecognizer.commandparameterproperty, new binding(/* here's i'm confused */)); var grid = new grid(); grid.gesturerecognizers.add(gridtap);
my confusion comes in on binding of commandparameterproperty
. in xaml, {binding}
no other parameter. how done in code? passing in new binding()
or this.bindingcontext
don't seem work.
the commandproperty
binding same doing.
as not passing in path property use, commandparameterproperty
can't create empty binding
throw exception.
to around need specify source
property adam has pointed out.
note, if bindingcontext
trying assign is null
, suspect in case, still throw exception.
the grid
in example below has bindingcontext
set view model (objgrid.bindingcontext = objmyview2
).
we creating new binding our command parameter, source
pointing our view model (source = objgrid.bindingcontext
).
if run demo below, see debug message in output
window indicating property value view model.
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);
supporting classes:-
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 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)); } } }
Comments
Post a Comment