gpt4 book ai didi

c# - 更改 WPF 数据网格中滚动条的位置

转载 作者:行者123 更新时间:2023-11-30 22:32:56 34 4
gpt4 key购买 nike

我的 C# WPF 应用程序中的数据网格存在布局问题。如果我的数据网格的内容因为行太多而无法显示在我的数据网格中,则会显示一个滚动查看器(和滚动条)。这是正确的方法,我很高兴。

问题是,滚动查看器在我的数据网格中的位置是错误的。滚动查看器从标题行开始,但滚动条显示在我的内容的第一行。在标题中有一个白色的 retangle。这是因为我的数据网格的背景是白色的。但是我的页眉背景是灰色的。

我上传了一张带有红色箭头的图片来澄清我的问题。白色的 retangle 看起来真的很难看,所以在我看来,这是更改滚动查看器位置的更好方法,因此它从内容的第一行开始。也许还有另一种可能来解决这个问题?

“带滚动查看器的数据网格”- 图片:

enter image description here

感谢任何提示,这将帮助我解决问题!

最好的问候,闪光器

最佳答案

你可以为你的数据网格制作你自己的样式,这里是一个混合了两个变化的样式

看看这两个变化

对于 PART_VerticalScrollBar -> Grid.Row="0"Grid.RowSpan="2"以及包含 PART_Horizo​​ntalScrollBar 的网格 -> Grid.ColumnSpan="2"

这是完整的样式

<Style x:Key="myGridStyle"
TargetType="{x:Type Controls:DataGrid}">
<Setter Property="Background"
Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
<Setter Property="Foreground"
Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
<Setter Property="BorderBrush"
Value="#FF688CAF" />
<Setter Property="BorderThickness"
Value="1" />
<Setter Property="RowDetailsVisibilityMode"
Value="VisibleWhenSelected" />
<Setter Property="ScrollViewer.CanContentScroll"
Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Controls:DataGrid}">
<Border SnapsToDevicePixels="True"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Padding="{TemplateBinding Padding}">
<ScrollViewer x:Name="DG_ScrollViewer"
Focusable="False">
<ScrollViewer.Template>
<ControlTemplate TargetType="{x:Type ScrollViewer}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Button Width="{Binding CellsPanelHorizontalOffset, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type Controls:DataGrid}}}"
Focusable="False">
<Button.Visibility>
<Binding Path="HeadersVisibility"
RelativeSource="{RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type Controls:DataGrid}}">
<Binding.ConverterParameter>
<Controls:DataGridHeadersVisibility>All</Controls:DataGridHeadersVisibility>
</Binding.ConverterParameter>
</Binding>
</Button.Visibility>
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Rectangle x:Name="Border"
Fill="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"
SnapsToDevicePixels="True" />
<Polygon x:Name="Arrow"
Fill="Black"
Stretch="Uniform"
HorizontalAlignment="Right"
Margin="8,8,3,3"
VerticalAlignment="Bottom"
Opacity="0.15"
Points="0,10 10,10 10,0" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver"
Value="True">
<Setter Property="Stroke"
TargetName="Border"
Value="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}" />
</Trigger>
<Trigger Property="IsPressed"
Value="True">
<Setter Property="Fill"
TargetName="Border"
Value="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}" />
</Trigger>
<Trigger Property="IsEnabled"
Value="False">
<Setter Property="Visibility"
TargetName="Arrow"
Value="Collapsed" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
<Button.Command>
<RoutedCommand />
</Button.Command>
</Button>
<Custom:DataGridColumnHeadersPresenter x:Name="PART_ColumnHeadersPresenter"
Grid.Column="1">
<Custom:DataGridColumnHeadersPresenter.Visibility>
<Binding Path="HeadersVisibility"
RelativeSource="{RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type Controls:DataGrid}}">
<Binding.ConverterParameter>
<Controls:DataGridHeadersVisibility>Column</Controls:DataGridHeadersVisibility>
</Binding.ConverterParameter>
</Binding>
</Custom:DataGridColumnHeadersPresenter.Visibility>
</Custom:DataGridColumnHeadersPresenter>
<ScrollContentPresenter x:Name="PART_ScrollContentPresenter"
Grid.ColumnSpan="2"
Grid.Row="1"
Content="{TemplateBinding Content}"
ContentStringFormat="{TemplateBinding ContentStringFormat}"
ContentTemplate="{TemplateBinding ContentTemplate}"
CanContentScroll="{TemplateBinding CanContentScroll}"
CanHorizontallyScroll="False"
CanVerticallyScroll="False" />
<ScrollBar x:Name="PART_VerticalScrollBar"
Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"
Grid.Column="2"
Grid.Row="0"
Grid.RowSpan="2"
Maximum="{TemplateBinding ScrollableHeight}"
Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"
Orientation="Vertical"
ViewportSize="{TemplateBinding ViewportHeight}" />
<Grid Grid.Column="1"
Grid.ColumnSpan="2"
Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding NonFrozenColumnsViewportHorizontalOffset, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type Controls:DataGrid}}}" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<ScrollBar x:Name="PART_HorizontalScrollBar"
Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"
Grid.Column="1"
Maximum="{TemplateBinding ScrollableWidth}"
Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"
Orientation="Horizontal"
ViewportSize="{TemplateBinding ViewportWidth}" />
</Grid>
</Grid>
</ControlTemplate>
</ScrollViewer.Template>
<ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsGrouping"
Value="True">
<Setter Property="ScrollViewer.CanContentScroll"
Value="False" />
</Trigger>
</Style.Triggers>
</Style>

希望对你有帮助

关于c# - 更改 WPF 数据网格中滚动条的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8526120/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com