WPF:如何在datagrid中冻结列标题

时间:2022-03-31 08:23:26

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.

如何在WPF窗口的DataGrid中冻结列标题,以便向下滚动时,标题仍然可见。

[Edit]

(编辑)

Here's my XAML:

这是我的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.

我只需要去掉ScrollViewer就可以了。

4 个解决方案

#1


16  

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

我只需要去掉ScrollViewer就可以了。

#2


0  

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

Datagrid具有FreeColumnCount属性——将其设置为1,看看会发生什么。

#3


0  

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

使用嵌套滚动查看器。下面是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.

当然,ScrollViewer也可以被样式化以反映您的UI设计。

#4


-2  

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

很难冻结DataGrid列,最好使用DataGridView

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

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

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

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

#1


16  

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

我只需要去掉ScrollViewer就可以了。

#2


0  

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

Datagrid具有FreeColumnCount属性——将其设置为1,看看会发生什么。

#3


0  

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

使用嵌套滚动查看器。下面是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.

当然,ScrollViewer也可以被样式化以反映您的UI设计。

#4


-2  

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

很难冻结DataGrid列,最好使用DataGridView

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

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

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

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