for-each
for-each

Reputation: 669

WPF: How to freeze column header in datagrid

How can I freeze my column header in a DataGrid in my WPF Window so that when I scroll down, the header is still visible.

[Edit]

Here's my XAML:

<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Visible">
    <DataGrid Name="ModelsGrid" Background="Transparent" Foreground="Black"  RowHeight="30" ColumnWidth="100"  AutoGenerateColumns="False" ItemsSource="{Binding}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Property ID" Binding="{Binding Path=Id}" />
            <DataGridTextColumn Header="Name" Width="Auto" Binding="{Binding Path=PropertyName}" />
            <DataGridTextColumn Header="Description" Width="Auto" Binding="{Binding Path=Description}" />
            <DataGridTextColumn Header="Access" Width="Auto" Binding="{Binding Path=Accessibility}" />
            <DataGridTextColumn Header="Type" Width="Auto" Binding="{Binding Path=Type}" />
            <DataGridTextColumn Header="Category" Width="Auto" Binding="{Binding Path=Category}" />
        </DataGrid.Columns>
    </DataGrid>
</ScrollViewer>

[Edit]

I just had to get rid of the ScrollViewer and it's solved.

Upvotes: 16

Views: 17705

Answers (4)

Andy Brown
Andy Brown

Reputation: 12999

You do it with nested scroll viewers. Here's the Template property setter for a Style of TargetType="DataGrid":

<Setter Property="Template">
  <Setter.Value>
    <ControlTemplate TargetType="DataGrid">

      <DockPanel Dock="Top" HorizontalAlignment="Stretch">

        <ScrollViewer DockPanel.Dock="Top" 
                      CanContentScroll="False" 
                      HorizontalScrollBarVisibility="Disabled" 
                      VerticalScrollBarVisibility="Disabled" 
                      Focusable="false" 
                      Padding="{TemplateBinding Padding}">

          <DockPanel Dock="Top" VerticalAlignment="Stretch">
            <DataGridColumnHeadersPresenter DockPanel.Dock="Top" Grid.Row="0"/>
            <ScrollViewer HorizontalScrollBarVisibility="Hidden" 
                          DockPanel.Dock="Top" 
                          VerticalScrollBarVisibility="Auto" 
                          VerticalAlignment="Stretch" 
                          CanContentScroll="False" 
                          Focusable="false" 
                          Padding="{TemplateBinding Padding}">
              <ItemsPresenter VerticalAlignment="Stretch"/>
            </ScrollViewer>
          </DockPanel>
        </ScrollViewer>
      </DockPanel>

    </ControlTemplate>
  </Setter.Value>
</Setter>

Of course the ScrollViewer can also be styled to reflect your UI design.

Upvotes: 0

Christian Sauer
Christian Sauer

Reputation: 10899

The Datagrid have FreeColumnCount property - set it to 1 and see what happens.

Upvotes: 0

for-each
for-each

Reputation: 669

I just had to get rid of the ScrollViewer and it's solved.

Upvotes: 26

Rocky
Rocky

Reputation: 4524

it is very hard to freeze DataGrid column, Better to use DataGridView for that

http://msdn.microsoft.com/en-us/library/28e9w2e1.aspx

http://msmvps.com/blogs/peterritchie/archive/2008/08/11/datagridviewcolumn-frozen.aspx

Upvotes: -4

Related Questions