다음을 통해 공유


DataGridColumn.Visibility 속성

정의

열의 표시 유형을 가져오거나 설정합니다.

public:
 property System::Windows::Visibility Visibility { System::Windows::Visibility get(); void set(System::Windows::Visibility value); };
public System.Windows.Visibility Visibility { get; set; }
member this.Visibility : System.Windows.Visibility with get, set
Public Property Visibility As Visibility

속성 값

Visibility

열 표시 여부를 지정하는 열거형 값입니다. 등록된 기본값은 Visible입니다. 값에 영향을 줄 수 있는 요소에 대한 자세한 내용은 DependencyProperty를 참조하십시오.

예제

다음 예에서는 열의 표시 유형을 설정 하는 방법을 보여 줍니다.

<CheckBox Content="View Customer Details" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked" />
<!-- ItemsSource is a DataTable that contains a list of customers.  The DataTable columns are  
     Title, FirstName, MiddleName, LastName, Suffix, CompanyName, EmailAddress, and Phone.-->
<DataGrid Grid.Row="1" Name="DG1" ItemsSource="{Binding}" AutoGeneratingColumn="DG1_AutoGeneratingColumn"  />
private void DG1_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    //Set properties on the columns during auto-generation
    switch (e.Column.Header.ToString())
    {
        case "LastName":
            e.Column.CanUserSort = false;
            e.Column.Visibility = Visibility.Visible;
            break;
        case "FirstName":
            e.Column.Visibility = Visibility.Visible;
            break;
        case "CompanyName":
            e.Column.Visibility = Visibility.Visible;
            break;
        case "EmailAddress":
            e.Column.Visibility = Visibility.Visible;
            break;
        default:
            e.Column.Visibility = Visibility.Collapsed;
            break;
    }
}

private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
    //Make each column in the collection visible
    foreach (DataGridColumn col in DG1.Columns)
    {
        col.Visibility = Visibility.Visible;
    }
}

private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
{
    //Get the columns collection
    ObservableCollection<DataGridColumn> columns = DG1.Columns;

    //set the visibility for each column so only 4 columns are visible
    foreach (DataGridColumn col in columns)
    {
        switch (col.Header.ToString())
        {
            case "LastName":
                col.Visibility = Visibility.Visible;
                break;
            case "FirstName":
                col.Visibility = Visibility.Visible;
                break;
            case "CompanyName":
                col.Visibility = Visibility.Visible;
                break;
            case "EmailAddress":
                col.Visibility = Visibility.Visible;
                break;
            default:
                col.Visibility = Visibility.Collapsed;
                break;
        }
    }
}
Private Sub DG1_AutoGeneratingColumn(ByVal sender As Object, ByVal e As DataGridAutoGeneratingColumnEventArgs)
    'Set properties on the columns during auto-generation
    Select Case e.Column.Header.ToString()
        Case "LastName"
            e.Column.CanUserSort = False
            e.Column.Visibility = Visibility.Visible
            Exit Select
        Case "FirstName"
            e.Column.Visibility = Visibility.Visible
            Exit Select
        Case "CompanyName"
            e.Column.Visibility = Visibility.Visible
            Exit Select
        Case "EmailAddress"
            e.Column.Visibility = Visibility.Visible
            Exit Select
        Case Else
            e.Column.Visibility = Visibility.Collapsed
            Exit Select

    End Select
End Sub

Private Sub CheckBox_Checked(ByVal sender As Object, ByVal e As RoutedEventArgs)
    'Make each column in the collection visible
    For Each col As DataGridColumn In DG1.Columns
        col.Visibility = Visibility.Visible
    Next
End Sub

Private Sub CheckBox_Unchecked(ByVal sender As Object, ByVal e As RoutedEventArgs)
    'Get the columns collection
    Dim columns As ObservableCollection(Of DataGridColumn) = DG1.Columns

    'set the visibility for each column so only 4 columns are visible
    For Each col As DataGridColumn In columns
        Select Case col.Header.ToString()
            Case "LastName"
                col.Visibility = Visibility.Visible
                Exit Select
            Case "FirstName"
                col.Visibility = Visibility.Visible
                Exit Select
            Case "CompanyName"
                col.Visibility = Visibility.Visible
                Exit Select
            Case "EmailAddress"
                col.Visibility = Visibility.Visible
                Exit Select
            Case Else
                col.Visibility = Visibility.Collapsed
                Exit Select
        End Select

    Next
End Sub

설명

설정 된 열을 숨기려면 해당 Visibility 속성을 Collapsed입니다.

적용 대상

추가 정보