programing

그리드 행 및 열 위치를 프로그래밍 방식으로 설정하는 방법

subpage 2023. 4. 29. 09:20
반응형

그리드 행 및 열 위치를 프로그래밍 방식으로 설정하는 방법

스택 패널 안에 두 개의 그리드가 있습니다.첫 번째 그리드의 이름은 GridX입니다.처음에는 그리드 내부에 2D 텍스트 상자(행 정의/열 정의) 배열이 있습니다.XAML의 TextBox 정의는 다음과 같습니다.

<TextBox x:Name="A1" Grid.Row="4" Grid.Column="5" TextAlignment="Center" />

GridX의 일부와 동일한 위치에 TextBlock을 프로그래밍 방식으로 추가하고 싶습니다.

효과는 다음과 같아야 합니다.

<TextBlock Grid.Row="4" Grid.Column="5"
HorizontalAlignment="Left" VerticalAlignment="Top" Text="10" FontSize="8"/>

이거 추가하는 방법.시도해 본 적이 있습니다.

TextBlock tblock = new TextBlock();
GridX.SetColumn(tblock, cIndex);
GridX.SetRow(tblock, rIndex);

하지만 실패했습니다.

다시 시도했습니다.

int rIndex = Grid.GetRow(txtBox);
int cIndex = Grid.GetColumn(txtBox);                               

TextBlock tblock = new TextBlock();
tblock.VerticalAlignment = VerticalAlignment.Top;
tblock.HorizontalAlignment = HorizontalAlignment.Left;
tblock.FontSize = 8;
tblock.Text = rc[i, j - 1];

Grid.SetColumn(tblock, cIndex);
Grid.SetRow(tblock, rIndex);

txtBox.MaxLength = 1;    

이제 문제는 TextBlock이 보이지 않는다는 것입니다.텍스트 상자가 숨깁니다.고마웠어.

연결된 속성의 경우 값을 할당할 개체에 대해 SetValue를 호출할 수 있습니다.

tblock.SetValue(Grid.RowProperty, 4);

또는 소유자 유형의 속성(이 경우 SetRow:

Grid.SetRow(tblock, 4);

다음은 누군가에게 도움이 될 수 있는 예입니다.

Grid test = new Grid();
test.ColumnDefinitions.Add(new ColumnDefinition());
test.ColumnDefinitions.Add(new ColumnDefinition());
test.RowDefinitions.Add(new RowDefinition());
test.RowDefinitions.Add(new RowDefinition());
test.RowDefinitions.Add(new RowDefinition());

Label t1 = new Label();
t1.Content = "Test1";
Label t2 = new Label();
t2.Content = "Test2";
Label t3 = new Label();
t3.Content = "Test3";
Label t4 = new Label();
t4.Content = "Test4";
Label t5 = new Label();
t5.Content = "Test5";
Label t6 = new Label();
t6.Content = "Test6";

Grid.SetColumn(t1, 0);
Grid.SetRow(t1, 0);
test.Children.Add(t1);

Grid.SetColumn(t2, 1);
Grid.SetRow(t2, 0);
test.Children.Add(t2);

Grid.SetColumn(t3, 0);
Grid.SetRow(t3, 1);
test.Children.Add(t3);

Grid.SetColumn(t4, 1);
Grid.SetRow(t4, 1);
test.Children.Add(t4);

Grid.SetColumn(t5, 0);
Grid.SetRow(t5, 2);
test.Children.Add(t5);

Grid.SetColumn(t6, 1);
Grid.SetRow(t6, 2);
test.Children.Add(t6);
for (int i = 0; i < 6; i++)
{
    test.ColumnDefinitions.Add(new ColumnDefinition());

    Label t1 = new Label();
    t1.Content = "Test" + i;

    Grid.SetColumn(t1, i);
    Grid.SetRow(t1, 0);
    test.Children.Add(t1);
}

사용해 보십시오.

                Grid grid = new Grid(); //Define the grid
                for (int i = 0; i < 36; i++) //Add 36 rows
                {
                    ColumnDefinition columna = new ColumnDefinition()
                    {
                        Name = "Col_" + i,
                        Width = new GridLength(32.5),
                    };
                    grid.ColumnDefinitions.Add(columna);
                }

                for (int i = 0; i < 36; i++) //Add 36 columns
                {
                    RowDefinition row = new RowDefinition();
                    row.Height = new GridLength(40, GridUnitType.Pixel);
                    grid.RowDefinitions.Add(row);
                }

                for (int i = 0; i < 36; i++)
                {
                    for (int j = 0; j < 36; j++)
                    {
                        Label t1 = new Label()
                        {
                            FontSize = 10,
                            FontFamily = new FontFamily("consolas"),
                            FontWeight = FontWeights.SemiBold,
                            BorderBrush = Brushes.LightGray,
                            BorderThickness = new Thickness(2),
                            HorizontalContentAlignment = HorizontalAlignment.Center,
                            VerticalContentAlignment = VerticalAlignment.Center,
                        };
                        Grid.SetRow(t1, i);
                        Grid.SetColumn(t1, j);
                        grid.Children.Add(t1); //Add the Label Control to the Grid created
                    }
                }

언급URL : https://stackoverflow.com/questions/3745594/how-to-set-grid-row-and-column-positions-programmatically

반응형