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 に設定すると、ディスプレイ内の 2 つの左側の列が固定されます。 列が既に固定されているかどうかを確認するには、 の プロパティを IsFrozen 確認します DataGridColumn

適用対象

こちらもご覧ください