다음을 통해 공유


방법: Grid의 자식 요소 위치 지정

업데이트: 2007년 11월

이 예제에서는 Grid에 정의된 get 및 set 메서드를 사용하여 자식 요소의 위치를 지정하는 방법을 보여 줍니다.

예제

다음 예제에서는 열과 행이 각각 세 개씩 있는 부모 Grid 요소(grid1)를 정의합니다. 자식 Rectangle 요소(rect1)는 Grid의 0열, 0행에 추가됩니다. Button 요소는 Grid 내에서 Rectangle 요소의 위치를 다시 지정하기 위해 호출할 수 있는 메서드를 보여 줍니다. 사용자가 단추를 클릭하면 관련 메서드가 활성화됩니다.

    <StackPanel Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left" Orientation="Vertical">
      <Button Click="setCol0">Move Rectangle to Column 0</Button>
      <Button Click="setCol1">Move Rectangle to Column 1</Button>
      <Button Click="setCol2" Margin="0,0,0,10">Move Rectangle to Column 2</Button>
      <Button Click="setRow0">Move Rectangle to Row 0</Button>
      <Button Click="setRow1">Move Rectangle to Row 1</Button>
      <Button Click="setRow2" Margin="0,0,0,10">Move Rectangle to Row 2</Button>
      <Button Click="setColspan">Span All Columns</Button>
      <Button Click="setRowspan">Span All Rows</Button>
      <Button Click="clearAll">Clear All</Button>
    </StackPanel>
  </Grid>
<Grid DockPanel.Dock="Top" Margin="0,10,15,0" HorizontalAlignment="Left" Name="grid1" ShowGridLines="True" Width="400" Height="400" Background="LightSteelBlue">
  <Grid.ColumnDefinitions>
  <ColumnDefinition/>
  <ColumnDefinition/>
  <ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
  <RowDefinition/>
  <RowDefinition/>
  <RowDefinition/>
</Grid.RowDefinitions>

  <Rectangle Name="rect1" Fill="Silver" Grid.Column="0" Grid.Row="0"/>
  <TextBlock FontSize="15" HorizontalAlignment="Right" VerticalAlignment="Bottom" Grid.Column="0" Grid.Row="0" Margin="5">Column 0, Row 0</TextBlock>
  <TextBlock FontSize="15" HorizontalAlignment="Right" VerticalAlignment="Bottom" Grid.Column="1" Grid.Row="0" Margin="5">Column 1, Row 0</TextBlock>
  <TextBlock FontSize="15" HorizontalAlignment="Right" VerticalAlignment="Bottom" Grid.Column="2" Grid.Row="0" Margin="5">Column 2, Row 0</TextBlock>
  <TextBlock FontSize="15" HorizontalAlignment="Right" VerticalAlignment="Bottom" Grid.Column="0" Grid.Row="1" Margin="5">Column 0, Row 1</TextBlock>
  <TextBlock FontSize="15" HorizontalAlignment="Right" VerticalAlignment="Bottom" Grid.Column="1" Grid.Row="1" Margin="5">Column 1, Row 1</TextBlock>
  <TextBlock FontSize="15" HorizontalAlignment="Right" VerticalAlignment="Bottom" Grid.Column="2" Grid.Row="1" Margin="5">Column 2, Row 1</TextBlock>
  <TextBlock FontSize="15" HorizontalAlignment="Right" VerticalAlignment="Bottom" Grid.Column="0" Grid.Row="2" Margin="5">Column 0, Row 2</TextBlock>
  <TextBlock FontSize="15" HorizontalAlignment="Right" VerticalAlignment="Bottom" Grid.Column="1" Grid.Row="2" Margin="5">Column 1, Row 2</TextBlock>
  <TextBlock FontSize="15" HorizontalAlignment="Right" VerticalAlignment="Bottom" Grid.Column="2" Grid.Row="2" Margin="5">Column 2, Row 2</TextBlock>
</Grid>

다음 코드 숨김 예제에서는 단추 Click 이벤트가 발생시키는 메서드를 처리합니다. 이 예제에서는 관련 get 메서드를 사용하는 TextBlock 요소에 대해 이러한 메서드 호출을 작성하여 새 속성 값을 문자열로 출력합니다.

Private Sub setCol0(ByVal sender As Object, ByVal e As RoutedEventArgs)
    Grid.SetColumn(rect1, 0)
    txt1.Text = "Rectangle is in Column " + Grid.GetColumn(rect1).ToString()
End Sub

Private Sub setCol1(ByVal sender As Object, ByVal e As RoutedEventArgs)
    Grid.SetColumn(rect1, 1)
    txt1.Text = "Rectangle is in Column " + Grid.GetColumn(rect1).ToString()
End Sub

Private Sub setCol2(ByVal sender As Object, ByVal e As RoutedEventArgs)
    Grid.SetColumn(rect1, 2)
    txt1.Text = "Rectangle is in Column " + Grid.GetColumn(rect1).ToString()
End Sub

Private Sub setRow0(ByVal sender As Object, ByVal e As RoutedEventArgs)
    Grid.SetRow(rect1, 0)
    txt2.Text = "Rectangle is in Row " + Grid.GetRow(rect1).ToString()
End Sub

Private Sub setRow1(ByVal sender As Object, ByVal e As RoutedEventArgs)
    Grid.SetRow(rect1, 1)
    txt2.Text = "Rectangle is in Row " + Grid.GetRow(rect1).ToString()
End Sub

Private Sub setRow2(ByVal sender As Object, ByVal e As RoutedEventArgs)
    Grid.SetRow(rect1, 2)
    txt2.Text = "Rectangle is in Row " + Grid.GetRow(rect1).ToString()
End Sub

Private Sub setColspan(ByVal sender As Object, ByVal e As RoutedEventArgs)
    Grid.SetColumnSpan(rect1, 3)
    txt3.Text = "ColumnSpan is set to " + Grid.GetColumnSpan(rect1).ToString()
End Sub
Private Sub setRowspan(ByVal sender As Object, ByVal e As RoutedEventArgs)
    Grid.SetRowSpan(rect1, 3)
    txt4.Text = "RowSpan is set to " + Grid.GetRowSpan(rect1).ToString()
End Sub
     private void setCol0(object sender, RoutedEventArgs e) 
        {
            Grid.SetColumn(rect1, 0);
            txt1.Text = "Rectangle is in Column " + Grid.GetColumn(rect1).ToString();

        }
        private void setCol1(object sender, RoutedEventArgs e)
        {
            Grid.SetColumn(rect1, 1);
            txt1.Text = "Rectangle is in Column " + Grid.GetColumn(rect1).ToString();
        }
        private void setCol2(object sender, RoutedEventArgs e)
        {
            Grid.SetColumn(rect1, 2);
            txt1.Text = "Rectangle is in Column " + Grid.GetColumn(rect1).ToString();
        }
        private void setRow0(object sender, RoutedEventArgs e)
        {
            Grid.SetRow(rect1, 0);
            txt2.Text = "Rectangle is in Row " + Grid.GetRow(rect1).ToString();
        }
        private void setRow1(object sender, RoutedEventArgs e)
        {
            Grid.SetRow(rect1, 1);
            txt2.Text = "Rectangle is in Row " + Grid.GetRow(rect1).ToString();
        }
        private void setRow2(object sender, RoutedEventArgs e)
        {
            Grid.SetRow(rect1, 2);
            txt2.Text = "Rectangle is in Row " + Grid.GetRow(rect1).ToString();
        }
        private void setColspan(object sender, RoutedEventArgs e)
        {
            Grid.SetColumnSpan(rect1, 3);
            txt3.Text = "ColumnSpan is set to " + Grid.GetColumnSpan(rect1).ToString();
        }
        private void setRowspan(object sender, RoutedEventArgs e)
        {
            Grid.SetRowSpan(rect1, 3);
            txt4.Text = "RowSpan is set to " + Grid.GetRowSpan(rect1).ToString();
        }

전체 샘플을 보려면 모눈 위치 지정 메서드 샘플을 참조하십시오.

참고 항목

개념

Panel 개요

참조

Grid