【问题标题】:Scrolling Below ColumnHeader in WPF Datagrid在WPF Datagrid中的ColumnHeader下方滚动
【发布时间】:2017-02-06 20:29:15
【问题描述】:

当我将鼠标悬停在网格上并移动鼠标滚轮时,我试图让这个单列 DataGrid 滚动。

<Grid Height ="500" .....
<!-- ListCollectionView for sorting -->
<DataGrid Name="dgTable"  Style ="{DynamicResource DataGridStyle1}"
Grid.Row ="1" Grid.Column ="0"  
VerticalContentAlignment="Stretch"
ItemsSource="{Binding ItemsLcv}"
IsSynchronizedWithCurrentItem="True" 
SelectionMode="Single"
AutoGenerateColumns="False"
CanUserAddRows="False"
CanUserSortColumns="True"
HorizontalAlignment="Center"
Width="215" Height="500">

<DataGrid.Columns>
<DataGridTextColumn Header="Description" SortMemberPath="descr"
IsReadOnly="True" 
CanUserSort="True" 
Binding="{Binding Path=descr, Mode=TwoWay, UpdateSourceTrigger=     PropertyChanged}"
Width ="215"/>
</DataGrid.Columns>

这是 DataGrid 的样式。

<Style x:Key="DataGridStyle1" TargetType="DataGrid">
<Setter Property="RowHeaderWidth" Value="0" />
  <Setter Property = "ScrollViewer.VerticalScrollBarVisibility" Value="Hidden" />
  <Setter Property = "ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
 </Style>

这是 ColumnHeader 的样式:

<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="Height" Value="35" />
<Setter Property="SeparatorBrush" Value="#79858b" />
<Setter Property="FontWeight" Value="Black" />
<Setter Property="Background" Value ="{DynamicResource Brush1}"/> 
<Setter Property="BorderBrush" Value="#FF60727B"/>
<Setter Property="BorderThickness" Value="2,0,2,2"></Setter>
</Style>

当我将 DataGrid 放置在 ScrollViewer 标记中时,滚动工作正常,但 ColumnHeader 消失了。我需要保留 Columnheader 进行排序。
我已经查看了关于冻结行的 SO 帖子,Frozen rows in WPF DataGrid?。我希望有一个比在 ScrollViewer 组件上使用大量控件模板更简单的解决方案。提前谢谢你。

【问题讨论】:

  • DataGrid 有自己的滚动功能,可以按照您想要的方式工作。摆脱 ScrollViewer,并设置 DataGrid 的VerticalAlignment="Stretch"。不是VerticalContentAlignment,那是不同的属性。
  • 做到了。谢谢!

标签: c# wpf datagrid scrollviewer


【解决方案1】:

DataGrid 有自己的滚动功能,可以按照您想要的方式工作。

尝试摆脱ScrollViewer,并设置DataGrid 的VerticalAlignment="Stretch"。不是VerticalContentAlignment,那是不同的属性。

XAML 布局是一个错综复杂的小属性的迷宫,它们都是相似的,只是它们都不同。

【讨论】:

    猜你喜欢
    • 2023-03-05
    • 2017-07-12
    • 2012-05-23
    • 1970-01-01
    • 2014-06-24
    • 2011-10-06
    • 1970-01-01
    • 2011-01-19
    • 1970-01-01
    相关资源
    最近更新 更多