周末充电之WPF(三 ) .后台动态生成控件

布局 -连连看:

代码:

 1 private void Window_Loaded_1(object sender, RoutedEventArgs e)
 2         {
 3           
 4 
 5             //动态创建行
 6             for (int i = 0; i < 10; i++)
 7             {
 8                 //
 9                 ColumnDefinition cl = new ColumnDefinition();
10                 grid.ColumnDefinitions.Add(cl);
11 
12                 //
13                 RowDefinition rd = new RowDefinition();
14                 grid.RowDefinitions.Add(rd);
15             }
16             Random random = new Random();
17 
18             for (int i = 0; i < 10; i++)
19             {
20                 for (int j = 0; j < 10; j++)
21                 {
22                     int imgName = random.Next(1, 10);//>=1 & <10的随机数
23                    
24 
25                     Image img = new Image();
26                     img.Source = new BitmapImage(new Uri("imgs/"+imgName+".png",UriKind.Relative));
27                     Grid.SetRow(img, i);
28                     Grid.SetColumn(img, j);
29                     grid.Children.Add(img);
30                 }
31             }
32         }

 

posted @ 2015-05-31 12:12  许大虾  阅读(578)  评论(0编辑  收藏  举报