DataGrid.FrozenColumnCount 속성

정의

스크롤되지 않는 열의 수를 가져오거나 설정합니다.

public:
 property int FrozenColumnCount { int get(); void set(int value); };
public int FrozenColumnCount { get; set; }
member this.FrozenColumnCount : int with get, set
Public Property FrozenColumnCount As Integer

속성 값

Int32

스크롤할 수 없는 열 개수입니다. 등록 된 기본값은 0입니다. 값에 영향을 줄 수 있는 요소에 대한 자세한 내용은 DependencyProperty를 참조하십시오.

예제

다음 예제에서는 "열 고정"을 선택할 ContextMenu때 열을 고정하는 방법을 보여 있습니다. 열은 고정된 된 열에 포함 하기 위해 왼쪽으로 이동 됩니다.

<!--Defines the handlers for when the FreezeColumnCommand is executed-->
<Window.CommandBindings>
    <CommandBinding Command="{x:Static local:Window1.FreezeColumnCommand}" Executed="CommandBinding_Executed" />
</Window.CommandBindings>
<!--Defines the context menu for the ColumnHeaders and attaches the FreezeColumnCommand-->
<Window.Resources>
    <ContextMenu x:Key="ColumnHeaderMenu" IsEnabled="True"  >
        <MenuItem Header="Freeze Column" IsEnabled="True" Command="{x:Static local:Window1.FreezeColumnCommand}"
                  CommandTarget="{Binding RelativeSource={RelativeSource AncestorType=Popup}, Path=PlacementTarget}" 
                  CommandParameter="{Binding ElementName=DG1, Path=CurrentColumn.DisplayIndex}" />
    </ContextMenu>
</Window.Resources>

<Grid>
    <!--Creates a new DataGrid with a context menu for the column headers-->
    <DataGrid Name="DG1" ItemsSource="{Binding}"  >
        <DataGrid.ColumnHeaderStyle >
            <Style TargetType="DataGridColumnHeader">
                <Setter Property="ContextMenu" Value="{StaticResource ColumnHeaderMenu}" />
            </Style>
        </DataGrid.ColumnHeaderStyle>
    </DataGrid>
</Grid>
public partial class Window1 : Window
{
    
    public static RoutedUICommand FreezeColumnCommand = new RoutedUICommand();

    public Window1()
    {
        InitializeComponent();
        //GetData connects to the database and returns the data in a table.
        AdventureWorksLT2008DataSet.SalesOrderDetailDataTable dt = GetData();

        DG1.DataContext = dt;
    }
Class Window1
    Public Shared FreezeColumnCommand As New RoutedUICommand()

    Public Sub New()
        InitializeComponent()

        'GetData connects to the database and returns the data in a table.
        Dim dt As AdventureWorksLT2008DataSet.SalesOrderDetailDataTable = GetData()

        DG1.DataContext = dt
    End Sub
    private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
    {
       //Get the column header that started the command and move that column left to freeze it.
       System.Windows.Controls.Primitives.DataGridColumnHeader header = (System.Windows.Controls.Primitives.DataGridColumnHeader)e.OriginalSource;
       if (header.Column.IsFrozen ==true)
       {
           return;
       }
       else
       {
           header.Column.DisplayIndex = DG1.FrozenColumnCount;
           DG1.FrozenColumnCount++;
       }
    }
}
Private Sub CommandBinding_Executed(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
    'Get the column header that started the command and move that column left to freeze it.
    Dim header As System.Windows.Controls.Primitives.DataGridColumnHeader = DirectCast(e.OriginalSource, System.Windows.Controls.Primitives.DataGridColumnHeader)
    If header.Column.IsFrozen = True Then
        Exit Sub
    Else
        header.Column.DisplayIndex = DG1.FrozenColumnCount
        DG1.FrozenColumnCount += 1


    End If
End Sub

설명

고정 열은 항상 표시되는 열이며 표시 유형에서 스크롤할 수 없습니다. 고정 열은 항상 표시 순서의 맨 왼쪽 열입니다. 고정된 열을 고정되지 않은 열 그룹으로 끌거나 고정되지 않은 열을 고정된 열 그룹으로 끌 수 없습니다.

열을 고정하려면 속성을 설정합니다 FrozenColumnCount . 숫자로 지정된 맨 왼쪽 열이 FrozenColumnCount 고정됩니다. 예를 들어 2로 설정 FrozenColumnCount 하면 디스플레이의 왼쪽 열 두 개가 고정됩니다. 열이 이미 고정되어 있는지 확인하려면 열의 IsFrozen 속성을 DataGridColumn확인합니다.

적용 대상

추가 정보