728x90
반응형
WPF & C# - 열행 맞추어 Grid 생성하기 ( makeGrid / ColumnDefinitions / RowDefinitions ) |
MainWindow.xaml
<Window x:Class="makeGrid.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:makeGrid"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid x:Name="bgGrd">
</Grid>
</Window>
MainWindow.xaml.cs
public MainWindow()
{
InitializeComponent();
Init();
}
void Init()
{
// Grid + Label 추가하기
int numGrd = 0; // grid 갯수
for (int row = 0; row < 26; row++)
{
// 세로 행 생성
bgGrd.RowDefinitions.Add(new RowDefinition());
for (int col = 0; col < 10; col++)
{
// 가로 열 생성
if (row == 1) bgGrd.ColumnDefinitions.Add(new ColumnDefinition());
// Grid 생성하여 각 열에 넣기
if (numGrd == 256) break;
makeGrid(numGrd, col, row); // grd0, grd1, grd2
makeLabel(numGrd, col, row); // lbl0, lbl1, lbl2
numGrd++;
}
}
}
void makeGrid(int numGrd, int col, int row)
{
// Grid 생성
Grid grd = new Grid(); // grid 생성
grd.Name = "grd" + (++numGrd).ToString(); // 버튼명
// 각 위치에 넣기
bgGrd.Children.Add(grd);
Grid.SetColumn(grd, col);
Grid.SetRow(grd, row);
}
void makeLabel(int numGrd, int col, int row)
{
Label lbl = new Label(); // grid 생성
lbl.Name = "lbl" + (numGrd).ToString(); // 버튼명
lbl.Background = new SolidColorBrush(Colors.White);
lbl.BorderBrush = new SolidColorBrush(Colors.Red);
lbl.BorderThickness = new Thickness(1);
lbl.Padding = new Thickness(0);
lbl.Content = lbl.Name;
// 각 위치에 넣기
bgGrd.Children.Add(lbl);
Grid.SetColumn(lbl, col);
Grid.SetRow(lbl, row);
}
Grid 열과 행으로 나누기
728x90
반응형