c# - How to display gridlines in code behind -
here code:
grid gameboard = new grid(); gameboard.horizontalalignment = horizontalalignment.left; gameboard.verticalalignment = verticalalignment.top; gameboard.width = window.current.bounds.width; gameboard.height = window.current.bounds.width; border border = new border(); border.borderthickness = new thickness(1); border.borderbrush = new solidcolorbrush(colors.blue); (int j=0;j<7;j++) { gameboard.columndefinitions.add(new columndefinition()); } (int = 0; < 7; i++) { gameboard.rowdefinitions.add(new rowdefinition()); }
i learner, want show grid lines, can me? lot!
since learning, kick start efforts , others in similar situation next step.
start code following, , tweak it, learn it, research it, , of have fun.
xaml
<grid name="layoutroot" background="{themeresource applicationpagebackgroundthemebrush}" margin="30" horizontalalignment="stretch" verticalalignment="stretch"> </grid>
code
public mainpage() { this.initializecomponent(); datacontext = this; loaded += mainpage_loaded; } private void mainpage_loaded(object sender, routedeventargs e) { grid gameboard = new grid(); gameboard.horizontalalignment = horizontalalignment.stretch; gameboard.verticalalignment = verticalalignment.stretch; (int j = 0; j < 7; j++) { var cd = new columndefinition(); cd.width = new gridlength(1, gridunittype.star); var rd = new rowdefinition(); rd.height = new gridlength(1, gridunittype.star); gameboard.columndefinitions.add(cd); gameboard.rowdefinitions.add(rd); } (int j = 0; j < 7; j++) { (int = 0; < 7; i++) { border border = new border(); border.borderthickness = new thickness(1); border.borderbrush = new solidcolorbrush(colors.blue); border.horizontalalignment = horizontalalignment.stretch; border.verticalalignment = verticalalignment.stretch; var tb = new textblock(); tb.text = string.format($"i={i}; j={j}"); tb.margin = new thickness(4); grid.setcolumn(border, j); grid.setrow(border, i); border.child = tb; gameboard.children.add(border); } } layoutroot.children.add(gameboard); }
result
summary
it's start. it's not perfect, , inner borders not thicker edges take small amount of effort, should not difficult. hint: think how use border.borderthickness = new thickness(l, t, r, b);
l/t/r/b 1 or 0 depending on i/j. might make interview question; fun discussion.
Comments
Post a Comment