WPF:如何冻结数据网格中的列标题

时间:2013-04-25 06:54:36

标签: c# wpf xaml scrollview

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

[修改

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

[修改

我必须摆脱ScrollViewer并且它已经解决了。

4 个答案:

答案 0 :(得分:16)

我只是摆脱了ScrollViewer并且它已经解决了。

答案 1 :(得分:0)

Datagrid有FreeColumnCount property - 将其设置为1,看看会发生什么。

答案 2 :(得分:0)

您可以使用嵌套滚动查看器来执行此操作。这是Template Style TargetType="DataGrid"的{​​{1}}属性设置器:

<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>

当然,ScrollViewer也可以设计为反映您的UI设计。

答案 3 :(得分:-2)