改变一个WPF Datagrid中滚动条的位置 [英] Change the position of the scrollbar in a WPF Datagrid

查看:2555
本文介绍了改变一个WPF Datagrid中滚动条的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的C#WPF应用程序在DataGrid的布局问题。如果因为有很多行我的DataGrid的内容无法显示在我的DataGrid中,则显示的ScrollViewer(和滚动条)。这是正确的做法,我很开心。

I have a layout problem with a datagrid in my C# WPF application. If the content of my datagrid can't be displayed in my datagrid because there are to many rows, a scrollviewer (and scrollbar) is displayed. This is the correct way and I'm happy about it.

现在的问题是,该ScrollViewer中的数据网格我IST的位置不对。该ScrollViewer中开始的标题行中,但滚动条显示我的内容的第一排。在头有一个白色的retangle。这是因为我的DataGrid的背景是白色的。但我的头的背景是灰色的。

The problem is, that the position of the scrollviewer in my datagrid ist wrong. The scrollviewer starts in the header row, but the Scrollbar is displayed in the first row of my content. In the header there is a white retangle. This is because the background of my datagrid is white. But the background of my header is gray.

我上传了照片用红色箭头澄清我的问题。白色retangle看起来非常难看,所以在我看来,这是更好的方法来改变的ScrollViewer的位置,所以它的内容的第一行开始。 ?也许还有另外一种可能解决问题。

I uploaded a picture with a red arrow to clarify my problem. The white retangle looks really ugly, so in my opinion it's the better way to change the position of the scrollviewer, so it starts in the first row of the content. Maybe there is another possibility to solve the problem?

在DataGrid中的ScrollViewer - 图像:

"Datagrid with Scrollviewer"-Image:

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

Thanks for any hints, which will help me to solve the problem!

最好的问候,
闪光

Best regards, Flasher

推荐答案

你可以让你自己的风格为你的数据网格,这里是混合制成有两个变化的风格

you can make your own style for your datagrid, here is a style made with blend with two changes

看这两个变化

PART_VerticalScrollBar - > Grid.Row =0 Grid.RowSpan =2
和用于保存在 PART_Horizo​​ntalScrollBar 格 - > Grid.ColumnSpan =2

for PART_VerticalScrollBar -> Grid.Row="0" and Grid.RowSpan="2" and for the grid that holds the 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>



希望这有助于

hope this helps

这篇关于改变一个WPF Datagrid中滚动条的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆