How to: Add Control to Grid in Code behind in Windows Store App

Hello,

 

you can add a button in code behind to a Grid that was defined in xaml.

 

First you have to give your Grid in xaml a name.

I named mine “ButtonGrid”.

 

<Grid Name="ButtonGrid">

 

After that you can create your control in code behind.

 

Button b = new Button();

b.Style = style;
b.Content = item;
b.Click += OnKey;
b.SetValue(
Grid.RowProperty, row);
b.SetValue(
Grid.ColumnProperty, column);

ButtonGrid.Children.Add(b);

 

 

You can apply styles to that button, too.

 

var style = Application.Current.Resources["NAMEOFYOURSTYLE"] as Style;

     
if (style == null)
          b.Style = style;


Posted Jul 02 2014, 01:52 PM by Holger Vetter
Filed under:
developers.de is a .Net Community Blog powered by daenet GmbH.