我的C#WPF应用程序中存在数据网格布局问题。如果我的数据网格的内容无法在我的数据网格中显示,因为有很多行,则会显示滚动查看器(和滚动条)。这是正确的方法,我很高兴。
问题是,我的数据网格中的scrollviewer的位置是错误的。 scrollviewer从标题行开始,但Scrollbar显示在我的内容的第一行。在标题中有一个白色的纠结。这是因为我的数据网格的背景是白色的。但我标题的背景是灰色的。
我上传了一张带有红色箭头的照片,以澄清我的问题。白色的视角看起来很难看,所以在我看来这是更改滚动查看器位置的更好方法,所以它从内容的第一行开始。也许有另一种可能性来解决这个问题?
“带Scrollviewer的数据网格”-Image:
感谢任何提示,这将有助于我解决问题!
祝你好运, 闪蒸器
答案 0 :(得分:3)
你可以为你的数据网格创建自己的风格,这里是一个混合了两个变化的风格
看看这两个变化
代表PART_VerticalScrollBar
- > Grid.Row="0"
和Grid.RowSpan="2"
并且对于包含PART_HorizontalScrollBar
- >的网格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>
希望这会有所帮助
答案 1 :(得分:0)
我一直很讨厌这个角落。最简单的方法是将DataGrid.Background
颜色设置为标题颜色,尽管如果它具有固定大小,它也会为空DataGrid
的背景着色。您可以将DataGrid
置于不会拉伸其子项的控件内,例如StackPanel
或带有DockPanel
的{{1}}
LastChildFill="False"
替代方案包括覆盖DataGrid的样式或模板片段或它的ScrollBars。凌乱,但可能。
答案 2 :(得分:0)
将您的数据网格包含在Scrollviewer中,并将其horizontalScrollBarVisibility设置为hidden 和VerticalScrollBarVisibility到Auto.Hope这将有所帮助。