c# - How Do I bind a button Content with a list of numbers in WP8? -
at first let me tell you, want achieve. want when windows page loads create lot of buttons, 10 buttons @ run time want button.content bind listvalues, list of 10 numbers.
public list<int> listvalues = new list<int>();
i want in mvvm, approach in model have public int listnumbers property, onpropertychanged event defined. in view model, how fill list listvalues 10 integer values(exactly new mvvm that's why asking). ten values used content of 10 buttons run time generated. , after filling listvaluesin mainpage_loaded method of mainpage how bind content of button listvalues .
to better understand requirement...
i have below xaml code
<canvas x:name="gamecanvas" background="bisque" height="480" width="480" /> in mainpage.xaml
so in code behind
int locationfirst = 25; int locationsecond = 100; char seatvalue = 'a'; int row = 3; int column = 3; public gamepage() { initializecomponent(); (int x = 1; x <= row; x++) { (int = 1; <= column; i++) { createbuttons(seatvalue.tostring() + i, locationfirst, locationsecond); locationfirst = locationfirst + 130; } locationfirst = 25; locationsecond = locationsecond + 50; } } the createbuttons code
button btnnew = new button(); btnnew.name = btnname; btnnew.margin = new thickness(btnpointfirst, btnpointsecond, 0, 0); btnnew.width = 100; btnnew.height = 70; gamecanvas.children.add(btnnew); in windows phone found issue, there no btnnew.location(x,y);
so @ run time have use btnnew.margin = new thickness(btnpointfirst, btnpointsecond, 0, 0); not putting buttons in desired location. code how assign btnnew.content listnumbers value?
please help.
any link or elaborate answer fine me...
thanks
i hope understood 1
public gamepage() { initializecomponent(); this.datacontext = gamepageviewmodel(); (int x = 1; x <= row; x++) { (int = 1; <= column; i++) { createbuttons(seatvalue.tostring() + i, locationfirst, locationsecond); locationfirst = locationfirst + 130; } locationfirst = 25; locationsecond = locationsecond + 50; } } public class gamepageviewmodel { //list of numbers put e.g. list<int> //change propertynameoftheviewmodel here properties, 10 properties e.g, public int content { get; set; } } button btnnew = new button(); btnnew.name = btnname; btnnew.margin = new thickness(btnpointfirst, btnpointsecond, 0, 0); btnnew.width = 100; btnnew.height = 70; btnnew.setbinding(button.contentproperty,new binding("propertynameoftheviewmodel"); but then, wouldn't recommend doing kind of stuff in viewmodel because unnecessary, use binding viewmodel if 1 planning show in ui coming database/business logic. setting button contents 1-10 not gonna in viewmodel, if continue you'll end unnecessary code in viewmodel breaking mvvm pattern.
my question is, why have binding? set button content when creating in mainpage.xaml.cs, correct. adding unnecessary layer set button's content.
Comments
Post a Comment