WPF 动态布局Grid

时间:2021-09-07 03:26:33
 //开启线程加载
Action a = () =>
{
var row = ;
var column = ; var path = "../../face_img/";
var files = Directory.GetFiles(path);
for (var i = ; i < row; i++)
{
faceGrid.RowDefinitions.Add(new RowDefinition());
}
for (var i = ; i < column; i++)
{
faceGrid.ColumnDefinitions.Add(new ColumnDefinition());
}
var index = ;
for (var i = ; i < row; i++)
{
for (var j = ; j < column; j++, index++)
{
if (index < files.Length)
{
var btn = new Button { Width = , Height = };
var image = new GifImage { Source = System.IO.Path.GetFullPath(files[index]), Stretch = Stretch.Fill };
btn.Click += btn_Click;
image.ToolTip = "["+ System.IO.Path.GetFileName(files[index]) + "]";
btn.Tag = "[" + System.IO.Path.GetFileName(files[index]) + "]";
image.OpacityMask = this.Background;
image.Width = btn.Width-;
image.Height = btn.Height-;
btn.BorderBrush = Brushes.WhiteSmoke;
btn.Background = Brushes.White;
btn.Content = image;
Grid.SetRow(btn, i);
Grid.SetColumn(btn, j); faceGrid.Children.Add(btn);
}
}
}
};
this.faceGrid.Dispatcher.Invoke(a);

layout