방법: ColumnDefinitionsCollections 및 RowDefinitionsCollections를 사용하여 열 및 행 조작
이 예제에서는 ColumnDefinitionCollection 및 RowDefinitionCollection 클래스의 메서드를 사용하여 행 또는 열의 내용을 추가, 삭제, 계산하는 등의 작업을 수행하는 방법을 보여 줍니다. 예를 들어 ColumnDefinition 또는 RowDefinition에 포함된 항목을 Add, Clear 또는 Count할 수 있습니다.
예제
다음 예제에서는 grid1
의 Name을 사용하여 Grid 요소를 만듭니다. Grid에는 각각 다른 수집 메서드에 의해 제어되는 Button 요소를 보유하는 StackPanel이 포함됩니다. Button을 클릭하면 코드 숨김 파일에서 메서드 호출이 활성화됩니다.
<DockPanel Margin="10,0,0,0">
<TextBlock FontSize="20" FontWeight="Bold" DockPanel.Dock="Top">Grid Column and Row Collections</TextBlock>
<TextBlock DockPanel.Dock="Top">Click any of the buttons below to invoke the associated methods and properties of ColumnDefinition and RowDefinition Collections.</TextBlock>
<Grid DockPanel.Dock="Top" HorizontalAlignment="Left" Name="grid1" ShowGridLines="true" Width="625" Height="400" Background="#b0e0e6">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
</Grid>
<StackPanel HorizontalAlignment="Left" Orientation="Horizontal" Width="625" DockPanel.Dock="Top">
<Button Width="125" Click="addCol">Add Column</Button>
<Button Width="125" Click="addRow">Add Row</Button>
<Button Width="125" Click="clearCol">Clear All Columns</Button>
<Button Width="125" Click="clearRow">Clear All Rows</Button>
<Button Width="125" Click="removeCol">Remove One Column</Button>
</StackPanel>
<StackPanel HorizontalAlignment="Left" Orientation="Horizontal" Width="625" DockPanel.Dock="Top">
<Button Width="125" Click="removeRow">Remove One Row</Button>
<Button Width="125" Click="colCount">How Many Columns?</Button>
<Button Width="125" Click="rowCount">How Many Rows?</Button>
<Button Width="125" Click="rem5Col">Remove 5 Columns</Button>
<Button Width="125" Click="rem5Row">Remove 5 Rows</Button>
</StackPanel>
<StackPanel HorizontalAlignment="Left" Orientation="Horizontal" Width="625" DockPanel.Dock="Top">
<Button Width="125" Click="containsRow">Contains Row?</Button>
<Button Width="125" Click="containsCol">Contains Column?</Button>
<Button Width="125" Click="insertRowAt">Insert Row</Button>
<Button Width="125" Click="insertColAt">Insert Column</Button>
<Button Width="125" Click="colReadOnly">IsReadOnly?</Button>
</StackPanel>
<TextBlock DockPanel.Dock="Top" Name="tp1"/>
<TextBlock DockPanel.Dock="Top" Name="tp2"/>
<TextBlock DockPanel.Dock="Top" Name="tp3"/>
<TextBlock DockPanel.Dock="Top" Name="tp4"/>
<TextBlock DockPanel.Dock="Top" Name="tp5"/>
</DockPanel>
이 예제에서는 각각 XAML(Extensible Application Markup Language) 파일의 Click 이벤트에 해당하는 일련의 사용자 지정 메서드를 정의합니다. 행 및 열을 추가 또는 제거하고 행 및 열의 총 수를 계산하는 등 여러 가지 방법으로 Grid의 열 및 행 수를 변경할 수 있습니다. ArgumentOutOfRangeException 및 ArgumentException 예외를 방지하기 위해 RemoveRange 메서드에서 제공하는 오류 검사 기능을 사용할 수 있습니다.
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
namespace columndefinitions_grid
{
public partial class Window1 : Window
{
RowDefinition rowDef1;
ColumnDefinition colDef1;
private void addCol(object sender, RoutedEventArgs e)
{
colDef1 = new ColumnDefinition();
grid1.ColumnDefinitions.Add(colDef1);
}
private void addRow(object sender, RoutedEventArgs e)
{
rowDef1 = new RowDefinition();
grid1.RowDefinitions.Add(rowDef1);
}
private void clearCol(object sender, RoutedEventArgs e)
{
grid1.ColumnDefinitions.Clear();
}
private void clearRow(object sender, RoutedEventArgs e)
{
grid1.RowDefinitions.Clear();
}
private void removeCol(object sender, RoutedEventArgs e)
{
if (grid1.ColumnDefinitions.Count <= 0)
{
tp1.Text = "No More Columns to Remove!";
}
else
{
grid1.ColumnDefinitions.RemoveAt(0);
}
}
private void removeRow(object sender, RoutedEventArgs e)
{
if (grid1.RowDefinitions.Count <= 0)
{
tp1.Text = "No More Rows to Remove!";
}
else
{
grid1.RowDefinitions.RemoveAt(0);
}
}
private void colCount(object sender, RoutedEventArgs e)
{
tp2.Text = "The current number of Columns is: " + grid1.ColumnDefinitions.Count;
}
private void rowCount(object sender, RoutedEventArgs e)
{
tp2.Text = "The current number of Rows is: " + grid1.RowDefinitions.Count;
}
private void rem5Col(object sender, RoutedEventArgs e)
{
if (grid1.ColumnDefinitions.Count < 5)
{
tp1.Text = "There aren't five Columns to Remove!";
}
else
{
grid1.ColumnDefinitions.RemoveRange(0,5);
}
}
private void rem5Row(object sender, RoutedEventArgs e)
{
if (grid1.RowDefinitions.Count < 5)
{
tp1.Text = "There aren't five Rows to Remove!";
}
else
{
grid1.RowDefinitions.RemoveRange(0, 5);
}
}
private void containsRow(object sender, RoutedEventArgs e)
{
if (grid1.RowDefinitions.Contains(rowDef1))
{
tp2.Text = "Grid Contains RowDefinition rowDef1";
}
else
{
tp2.Text = "Grid Does Not Contain RowDefinition rowDef1";
}
}
private void containsCol(object sender, RoutedEventArgs e)
{
if (grid1.ColumnDefinitions.Contains(colDef1))
{
tp3.Text = "Grid Contains ColumnDefinition colDef1";
}
else
{
tp3.Text = "Grid Does Not Contain ColumnDefinition colDef1";
}
}
private void colReadOnly(object sender, RoutedEventArgs e)
{
tp4.Text = "RowDefinitionsCollection IsReadOnly?: " + grid1.RowDefinitions.IsReadOnly.ToString();
tp5.Text = "ColumnDefinitionsCollection IsReadOnly?: " + grid1.ColumnDefinitions.IsReadOnly.ToString();
}
private void insertRowAt(object sender, RoutedEventArgs e)
{
rowDef1 = new RowDefinition();
grid1.RowDefinitions.Insert(grid1.RowDefinitions.Count, rowDef1);
tp1.Text = "RowDefinition added at index position " + grid1.RowDefinitions.IndexOf(rowDef1).ToString();
}
private void insertColAt(object sender, RoutedEventArgs e)
{
colDef1 = new ColumnDefinition();
grid1.ColumnDefinitions.Insert(grid1.ColumnDefinitions.Count, colDef1);
tp2.Text = "ColumnDefinition added at index position " + grid1.ColumnDefinitions.IndexOf(colDef1).ToString();
}
}
}
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Documents
Namespace SDKSample
'@ <summary>
'@ Interaction logic for Window1.xaml
'@ </summary>
Partial Public Class Window1
Inherits Window
Dim rowDef1 As New RowDefinition
Dim colDef1 As New ColumnDefinition
Private Sub addCol(ByVal sender As Object, ByVal e As RoutedEventArgs)
Dim colDef1 As New ColumnDefinition
grid1.ColumnDefinitions.Add(colDef1)
End Sub
Private Sub addRow(ByVal sender As Object, ByVal e As RoutedEventArgs)
Dim rowDef1 As New RowDefinition()
grid1.RowDefinitions.Add(rowDef1)
End Sub
Private Sub clearCol(ByVal sender As Object, ByVal e As RoutedEventArgs)
grid1.ColumnDefinitions.Clear()
End Sub
Private Sub clearRow(ByVal sender As Object, ByVal e As RoutedEventArgs)
grid1.RowDefinitions.Clear()
End Sub
Private Sub removeCol(ByVal sender As Object, ByVal e As RoutedEventArgs)
If (grid1.ColumnDefinitions.Count <= 0) Then
tp1.Text = "No More Columns to Remove!"
Else
grid1.ColumnDefinitions.RemoveAt(0)
End If
End Sub
Private Sub removeRow(ByVal sender As Object, ByVal e As RoutedEventArgs)
If (grid1.RowDefinitions.Count <= 0) Then
tp1.Text = "No More Rows to Remove!"
Else
grid1.RowDefinitions.RemoveAt(0)
End If
End Sub
Private Sub colCount(ByVal sender As Object, ByVal e As RoutedEventArgs)
tp2.Text = "The current number of Columns is: " + grid1.ColumnDefinitions.Count.ToString()
End Sub
Private Sub rowCount(ByVal sender As Object, ByVal e As RoutedEventArgs)
tp2.Text = "The current number of Rows is: " + grid1.RowDefinitions.Count.ToString()
End Sub
Private Sub rem5Col(ByVal sender As Object, ByVal e As RoutedEventArgs)
If (grid1.ColumnDefinitions.Count < 5) Then
tp1.Text = "There aren't five Columns to Remove!"
Else
grid1.ColumnDefinitions.RemoveRange(0, 5)
End If
End Sub
Private Sub rem5Row(ByVal sender As Object, ByVal e As RoutedEventArgs)
If (grid1.RowDefinitions.Count < 5) Then
tp1.Text = "There aren't five Rows to Remove!"
Else
grid1.RowDefinitions.RemoveRange(0, 5)
End If
End Sub
Private Sub containsRow(ByVal sender As Object, ByVal e As RoutedEventArgs)
If (grid1.RowDefinitions.Contains(rowDef1)) Then
tp2.Text = "Grid Contains RowDefinition rowDef1"
Else
tp2.Text = "Grid Does Not Contain RowDefinition rowDef1"
End If
End Sub
Private Sub containsCol(ByVal sender As Object, ByVal e As RoutedEventArgs)
If (grid1.ColumnDefinitions.Contains(colDef1)) Then
tp3.Text = "Grid Contains ColumnDefinition colDef1"
Else
tp3.Text = "Grid Does Not Contain ColumnDefinition colDef1"
End If
End Sub
Private Sub colReadOnly(ByVal sender As Object, ByVal e As RoutedEventArgs)
tp4.Text = "RowDefinitionsCollection IsReadOnly?: " + grid1.RowDefinitions.IsReadOnly.ToString()
tp5.Text = "ColumnDefinitionsCollection IsReadOnly?: " + grid1.ColumnDefinitions.IsReadOnly.ToString()
End Sub
Private Sub insertRowAt(ByVal sender As Object, ByVal e As RoutedEventArgs)
Dim rowDef1 As New RowDefinition
grid1.RowDefinitions.Insert(grid1.RowDefinitions.Count, rowDef1)
tp1.Text = "RowDefinition added at index position " + grid1.RowDefinitions.IndexOf(rowDef1).ToString()
End Sub
Private Sub insertColAt(ByVal sender As Object, ByVal e As RoutedEventArgs)
Dim colDef1 As New ColumnDefinition()
grid1.ColumnDefinitions.Insert(grid1.ColumnDefinitions.Count, colDef1)
tp2.Text = "ColumnDefinition added at index position " + grid1.ColumnDefinitions.IndexOf(colDef1).ToString()
End Sub
End Class
End Namespace
참고 항목
GitHub에서 Microsoft와 공동 작업
이 콘텐츠의 원본은 GitHub에서 찾을 수 있으며, 여기서 문제와 끌어오기 요청을 만들고 검토할 수도 있습니다. 자세한 내용은 참여자 가이드를 참조하세요.
.NET Desktop feedback