WPF 动态布局Grid

 1 //开启线程加载
 2             Action a = () =>
 3             {
 4                 var row = 7;
 5                 var column = 9;
 6 
 7                 var path = "../../face_img/";
 8                 var files = Directory.GetFiles(path);
 9                 for (var i = 0; i < row; i++)
10                 {
11                     faceGrid.RowDefinitions.Add(new RowDefinition());
12                 }
13                 for (var i = 0; i < column; i++)
14                 {
15                     faceGrid.ColumnDefinitions.Add(new ColumnDefinition());
16                 }
17                 var index = 0;
18                 for (var i = 0; i < row; i++)
19                 {
20                     for (var j = 0; j < column; j++, index++)
21                     {
22                         if (index < files.Length)
23                         {
24                             var btn = new Button { Width = 25, Height = 25 };
25                             var image = new GifImage { Source = System.IO.Path.GetFullPath(files[index]), Stretch = Stretch.Fill };
26                             btn.Click += btn_Click;
27                             image.ToolTip = "["+ System.IO.Path.GetFileName(files[index]) + "]";
28                             btn.Tag = "[" + System.IO.Path.GetFileName(files[index]) + "]";
29                             image.OpacityMask = this.Background;
30                             image.Width = btn.Width-1;
31                             image.Height = btn.Height-1;
32                             btn.BorderBrush = Brushes.WhiteSmoke;
33                             btn.Background = Brushes.White;
34                             btn.Content = image;
35                             Grid.SetRow(btn, i);
36                             Grid.SetColumn(btn, j);
37                          
38                             faceGrid.Children.Add(btn);
39                         }
40                     }
41                 }
42             };
43             this.faceGrid.Dispatcher.Invoke(a);
layout

 

posted @ 2014-06-26 10:43  b̶i̶n̶g̶.̶  阅读(1018)  评论(0编辑  收藏  举报