WPF:如何冻结数据网格中的列标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16208248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF: How to freeze column header in datagrid
提问by for-each
How can I freeze my column header in a DataGridin my WPFWindow so that when I scroll down, the header is still visible.
如何DataGrid在我的WPF窗口中冻结我的列标题,以便当我向下滚动时,标题仍然可见。
[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 ScrollViewerand it's solved.
我只需要摆脱ScrollViewer它就解决了。
回答by for-each
I just had to get rid of the ScrollViewer and it's solved.
我只需要摆脱 ScrollViewer 就解决了。
回答by Christian Sauer
The Datagrid have FreeColumnCount property- set it to 1 and see what happens.
Datagrid 具有FreeColumnCount 属性- 将其设置为 1 并查看会发生什么。
回答by Andy Brown
You do it with nested scroll viewers. Here's the Templateproperty setter for a Styleof TargetType="DataGrid":
您可以使用嵌套滚动查看器来完成。这是Templatea Styleof的属性设置器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 ScrollViewercan also be styled to reflect your UI design.
当然,ScrollViewer也可以设置样式以反映您的 UI 设计。
回答by Rocky
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

