共用方式為


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

屬性值

非捲動的資料行數目。 已註冊的預設值為 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 行將會凍結。 例如,如果您將 設定 FrozenColumnCount 為 2,顯示中的兩個左欄會凍結。 若要判斷資料行是否已凍結,請檢查 IsFrozen 上的 DataGridColumn 屬性。

適用於

另請參閱