如何在WPF中修改DataGrid垂直滚动条的位置? [英] How to modify the placement of DataGrid vertical scroll bar in WPF?

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

问题描述

SOLVED: 请使用XAML代码,屏幕截图和说明来查看我自己的答案。







我唯一能够找出的东西像它的外观)是将DataGrid的背景设置为透明。这是结果:





正如你所看到的,滚动条很烦躁。那么,如果背景是透明的,那么水平滚动条和最后一行之间也有差距问题:





还有一个解决方案可以摆脱这两个数据网格的背景颜色框,使它们脱颖而出:





但是我无法想像出一种方法来将其作为XAML中的边距中的负偏移值插入。在代码隐藏中这样做并不难,但我宁愿保留所有的XAML。无论如何,这里是进度和XAML代码部分的截图:





这是XAML代码部分:

 < Style TargetType ={x:Type DataGrid}> 
< Setter Property =Template>
< Setter.Value>
< ControlTemplate TargetType ={x:Type DataGrid}>
< Border BorderBrush ={TemplateBinding BorderBrush}BorderThickness ={TemplateBinding BorderThickness}Background ={TemplateBinding Background}Padding ={TemplateBinding Padding}SnapsToDevicePixels =True>
< ScrollViewer x:Name =DG_ScrollViewerFocusable =false>
< ScrollViewer.Template>
< ControlTemplate TargetType ={x:Type ScrollViewer}>
< Grid>
< Grid.ColumnDefinitions>
< ColumnDefinition Width =Auto/>
< ColumnDefinition Width =*/>
< ColumnDefinition Width =Auto/>
< /Grid.ColumnDefinitions>
< Grid.RowDefinitions>
< RowDefinition Height =Auto/>
< RowDefinition Height =*/>
< RowDefinition Height =Auto/>
< /Grid.RowDefinitions>
< Button Command ={x:Static DataGrid.SelectAllCommand}Focusable =falseStyle ={DynamicResource {ComponentResourceKey ResourceId = DataGridSelectAllButtonStyle,TypeInTargetAssembly = {x:Type DataGrid}}}Visibility ={ Binding HeadersVisibility,ConverterParameter = {x:Static DataGridHeadersVisibility.All},Converter = {x:Static DataGrid.HeadersVisibilityConverter},RelativeSource = {RelativeSource AncestorType = {x:Type DataGrid}}}Width ={Binding CellsPanelHorizo​​ntalOffset,RelativeSource = { RelativeSource AncestorType = {x:Type DataGrid}}}/>
< DataGridColumnHeadersPresenter x:Name =PART_ColumnHeadersPresenterGrid.Column =1Visibility ={Binding HeadersVisibility,ConverterParameter = {x:Static DataGridHeadersVisibility.Column},Converter = {x:Static DataGrid.HeadersVisibilityConverter} RelativeSource = {RelativeSource AncestorType = {x:Type DataGrid}}}/>
< ScrollContentPresenter Margin =0,0,17,0x:Name =PART_ScrollContentPresenterCanContentScroll ={TemplateBinding CanContentScroll}Grid.ColumnSpan =2Grid.Row =1/>
< ScrollBar Margin = - 17,0,0,0x:Name =PART_VerticalScrollBarGrid.Column =2Maximum ={TemplateBinding ScrollableHeight}Orientation =VerticalGrid.Row = 1Visibility ={TemplateBinding ComputedVerticalScrollBarVisibility}Value ={Binding VerticalOffset,Mode = OneWay,RelativeSource = {RelativeSource TemplatedParent}}ViewportSize ={TemplateBinding ViewportHeight}/>
< Grid Grid.Column =1Grid.Row =2>
< Grid.ColumnDefinitions>
< ColumnDefinition Width ={Binding NonFrozenColumnsViewportHorizo​​ntalOffset,RelativeSource = {RelativeSource AncestorType = {x:Type DataGrid}}}/>
< ColumnDefinition Width =*/>
< /Grid.ColumnDefinitions>
< ScrollBar x:Name =PART_Horizo​​ntalScrollBarGrid.Column =1Maximum ={TemplateBinding ScrollableWidth}Orientation =Horizo​​ntalVisibility ={TemplateBinding ComputedHorizo​​ntalScrollBarVisibility}Value ={Binding Horizo​​ntalOffset,Mode = OneWay,RelativeSource = {RelativeSource TemplatedParent}}ViewportSize ={TemplateBinding ViewportWidth}/>
< / Grid>
< / Grid>
< / ControlTemplate>
< /ScrollViewer.Template>
< ItemsPresenter SnapsToDevicePixels ={TemplateBinding SnapsToDevicePixels}/>
< / ScrollViewer>
< / Border>
< / ControlTemplate>
< /Setter.Value>
< / Setter>
< / Style>

使更改成为可能的重要部分是我设置Margin =的代码片段的一半 - 17,0,0,0为ScrollBar。



我目前的问题是,我无法弄清楚哪个组件偏移到左边17(通过添加边距或填充)。我已经搞砸了所有这些,没有运气到目前为止。一旦我得到它,我会更新,除非有人在我面前说出来。现在,滚动条将覆盖最后一列中的任何内容,直到我修正偏移量。



编辑#2:



请参考上面更新的XAML代码。我做的是向ScrollContentPresenter添加Margin =0,0,17,0。



我可以完全生活的一个副作用效果就是抵消标题的事实,但是直到你滚动一切就看不到在右边。它只影响最后一列的标题,因为ScrollContentPresenter也会偏移它...奇怪的是,由于有一个DataGridColumnHeadersPresenter,但它独立工作,所以我会继续工作。 ScrollContentPresenter,不幸的是没有填充,这将像一个魅力。所以,现在我必须弄清楚我可以如何填充它,而不是设置边距,或者找出一种不同的方法。



类似的方法是设置边距的水平ScrollBar(这是第二个网格中的)到0,0,-17,0。它将向右移动17个单位。然后,将DataGridColumnHeaderPresenter的边距设置为0,0,-17,0。它将向右移动17个单位。



编辑#3:



这是另一种有兴趣的方法:





将垂直ScrollBar的边距设置为0,-22,0,-17。 -22是我的标题的高度,所以调整到任何你的。此方法将滚动条展开以覆盖两个白框。



编辑#4:



我想出了解决方案。请看我的XAML代码的答案,屏幕截图和说明。

解决方案

所以,上面我已经玩了几个方法,但没有一个实现了我想要的完美的外观和填充。所以,这是我非常满意的解决方案。我希望它能帮助别人。我使用Blend通过模板代码的每一行,这是我目的的最好的组合。



最终结果:





赦免凌乱的头脑,我有意涵盖他们。您可以看到,垂直滚动条在标题和位于底部的水平滚动条之间很好地排列。这是通过将垂直ScrollBar(位于模板的第一个网格)的边距设置为左侧的-17来实现的。 Margin = - 17,0,0,0。然后,将模板代码中垂直滚动条上方的$ code> ScrollContentProperty 的边距设置为右侧的17, Margin =0 ,0,17,0 ,以确保垂直滚动条不覆盖任何内容,当我们滚动到最右边时,这将使我们在右边有17个单位的空白空间,这是因为我们在 ScrollContentPresenter 上的17保证金也会影响标题,为了克服这个原因,我创建了一个 DataGridColumnHeader 并将其边缘设置为右侧的-17, Margin =0,0,-17,0,确保其溢出到为了确保最后一个列标题文本不会悬停在垂直滚动条上,使其视觉上没有吸引力,我在右侧添加了一个17的填充, Padding =0,0,17 ,0



这是我的XAML代码,用于 DataGrid DataGridColumnHeader 那个疯了这可能是:



DataGrid:

 < Style TargetType ={x:Type DataGrid}> 
< Setter Property =Template>
< Setter.Value>
< ControlTemplate TargetType ={x:Type DataGrid}>
< Border BorderBrush ={TemplateBinding BorderBrush}BorderThickness ={TemplateBinding BorderThickness}Background ={TemplateBinding Background}Padding ={TemplateBinding Padding}SnapsToDevicePixels =True>
< ScrollViewer x:Name =DG_ScrollViewerFocusable =false>
< ScrollViewer.Template>
< ControlTemplate TargetType ={x:Type ScrollViewer}>
< Grid>
< Grid.ColumnDefinitions>
< ColumnDefinition Width =Auto/>
< ColumnDefinition Width =*/>
< ColumnDefinition Width =Auto/>
< /Grid.ColumnDefinitions>
< Grid.RowDefinitions>
< RowDefinition Height =Auto/>
< RowDefinition Height =*/>
< RowDefinition Height =Auto/>
< /Grid.RowDefinitions>
< Button Command ={x:Static DataGrid.SelectAllCommand}Focusable =falseStyle ={DynamicResource {ComponentResourceKey ResourceId = DataGridSelectAllButtonStyle,TypeInTargetAssembly = {x:Type DataGrid}}}Visibility ={ Binding HeadersVisibility,ConverterParameter = {x:Static DataGridHeadersVisibility.All},Converter = {x:Static DataGrid.HeadersVisibilityConverter},RelativeSource = {RelativeSource AncestorType = {x:Type DataGrid}}}Width ={Binding CellsPanelHorizo​​ntalOffset,RelativeSource = { RelativeSource AncestorType = {x:Type DataGrid}}}/>
< DataGridColumnHeadersPresenter x:Name =PART_ColumnHeadersPresenterGrid.Column =1Visibility ={Binding HeadersVisibility,ConverterParameter = {x:Static DataGridHeadersVisibility.Column},Converter = {x:Static DataGrid.HeadersVisibilityConverter} RelativeSource = {RelativeSource AncestorType = {x:Type DataGrid}}}/>
<! - 这是滚动内容主持人,转移到左侧17个单位,以便滚动条不覆盖它 - >
< ScrollContentPresenter Margin =0,0,17,0x:Name =PART_ScrollContentPresenterCanContentScroll ={TemplateBinding CanContentScroll}Grid.ColumnSpan =2Grid.Row =1/>
<! - 这是垂直滚动条。保证金用于将其转移到内容上的左侧17个单位 - >
< ScrollBar Margin = - 17,0,0,0x:Name =PART_VerticalScrollBarGrid.Column =2Maximum ={TemplateBinding ScrollableHeight}Orientation =VerticalGrid.Row = 1Visibility ={TemplateBinding ComputedVerticalScrollBarVisibility}Value ={Binding VerticalOffset,Mode = OneWay,RelativeSource = {RelativeSource TemplatedParent}}ViewportSize ={TemplateBinding ViewportHeight}/>
< Grid Grid.Column =1Grid.Row =2>
< Grid.ColumnDefinitions>
< ColumnDefinition Width ={Binding NonFrozenColumnsViewportHorizo​​ntalOffset,RelativeSource = {RelativeSource AncestorType = {x:Type DataGrid}}}/>
< ColumnDefinition Width =*/>
< /Grid.ColumnDefinitions>
< ScrollBar x:Name =PART_Horizo​​ntalScrollBarGrid.Column =1Maximum ={TemplateBinding ScrollableWidth}Orientation =Horizo​​ntalVisibility ={TemplateBinding ComputedHorizo​​ntalScrollBarVisibility}Value ={Binding Horizo​​ntalOffset,Mode = OneWay,RelativeSource = {RelativeSource TemplatedParent}}ViewportSize ={TemplateBinding ViewportWidth}/>
< / Grid>
< / Grid>
< / ControlTemplate>
< /ScrollViewer.Template>
< ItemsPresenter SnapsToDevicePixels ={TemplateBinding SnapsToDevicePixels}/>
< / ScrollViewer>
< / Border>
< / ControlTemplate>
< /Setter.Value>
< / Setter>
< / Style>

DataGridColumnHeader:

 < Style TargetType ={x:Type DataGridColumnHeader}> 
< Setter Property =HeightValue =22/>
<! - 填充将最后一列的标题文本移到左侧17个单位 - >
< Setter Property =PaddingValue =0,0,17,0/>
<! - 保证金将整个标题移到右边17个单位以填充空格 - >
< Setter Property =MarginValue =0,0,-17,0/>
< / Style>

类似的方法是将水平滚动条的边距设置为-17, Margin =0,0,-17,0,将DataGridColumnHeader的填充设置为17, Padding =0,0,17,0,其余额为-17, Margin =0,0,-17,0。这实现了相同的结果。


SOLVED: Please see my own answer below with the XAML code, a screenshot and an explanation.

It was a little tough to title this one, so let me explain what my issue is. I have a datagrid that has a defined height, so the scrollbar appears. I would like to contain the vertical scrollbar to the area that excludes the header. While it only scrolls the data rows and not the header, visually it covers the entire datagrid area to the right. The problem with that is that two boxes appear (one above and below) at the scrollbar area. I'm not sure how to get rid of them or how to contain the scrollbar to the body of the datagrid.

The only thing I've been able to figure out (and I don't like the look of it) is to set the Background of the DataGrid to Transparent. Here's the outcome:

As you see, the scrollbar is sticking out annoyingly. Then, there's also the problem of a gap between the horizontal scrollbar and the last row if the background is transparent:

There's also a solution that gets rid of the background color of the datagrid for those two boxes, making them stand out less:

http://social.msdn.microsoft.com/Forums/vstudio/en-US/9fc4252b-38b1-4369-8d76-b6c5ae1e4df5/how-to-remove-the-blank-space-above-the-verticalscroolbar-of-datagrid-in-wpf?forum=wpf

Similar solution can be found here: Annoying Square Where Scrollbars Meet

However, it doesn't get rid of the problem that the scrollbar is awkwardly sticking out on the side.

Something I tested was to separate header from the body and place the body into a vertical ScrollViewer, then place the header and the body into a horizontal ScrollViewer so that they can be both scrolled horizontally. But, as you imagine, this doesn't work out well, because you have to scroll to the right to see the verticall scrollbar. I'm sure there's a way to make it so that it stays frozen on the right, but I haven't figured it out. Another issue is the fact the header width has to match the largest possible width of any cell in that column, or everything will be off. Here's the outcome shown when scrolled all the way to the right:

I'm very new to control templates, so I can't figure out if this could work, because I can't find the right components:

If I give the vertical scrollbar a negative margin to the left (say -6,0,0,0) and a similarly sized padding to the right of the cell block (0,0,6,0), the vertical scrollbar technically should move to the left. I will continue experimenting and try to figure this out, unless someone has the answer for me (which would be awesome).

EDIT #1:

Well, I made some progress and was able to set the margin of the scrollbar to (-17,0,0,0). 17 appears to be the width of the scrollbar. There appears to be a specific key for its width:

http://msdn.microsoft.com/en-us/library/system.windows.systemparameters.verticalscrollbarwidthkey(v=vs.110).aspx

but I can't figured out a way to insert that as the negative offset value in the margin within XAML. It wouldn't be hard to do it in code-behind.... but I'd rather keep it all XAML. Anyways, here's the screenshot of the progress and the XAML code portion:

Here's XAML code portion:

    <Style TargetType="{x:Type DataGrid}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGrid}">
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True">
                        <ScrollViewer x:Name="DG_ScrollViewer" Focusable="false">
                            <ScrollViewer.Template>
                                <ControlTemplate TargetType="{x:Type ScrollViewer}">
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="Auto"/>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="Auto"/>
                                        </Grid.ColumnDefinitions>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition Height="*"/>
                                            <RowDefinition Height="Auto"/>
                                        </Grid.RowDefinitions>
                                        <Button Command="{x:Static DataGrid.SelectAllCommand}" Focusable="false" Style="{DynamicResource {ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}}" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.All}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Width="{Binding CellsPanelHorizontalOffset, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
                                        <DataGridColumnHeadersPresenter x:Name="PART_ColumnHeadersPresenter" Grid.Column="1" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Column}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
                                        <ScrollContentPresenter Margin="0,0,17,0" x:Name="PART_ScrollContentPresenter" CanContentScroll="{TemplateBinding CanContentScroll}" Grid.ColumnSpan="2" Grid.Row="1"/>
                                        <ScrollBar Margin="-17,0,0,0" x:Name="PART_VerticalScrollBar" Grid.Column="2" Maximum="{TemplateBinding ScrollableHeight}" Orientation="Vertical" Grid.Row="1" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportHeight}"/>
                                        <Grid Grid.Column="1" Grid.Row="2">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="{Binding NonFrozenColumnsViewportHorizontalOffset, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
                                                <ColumnDefinition Width="*"/>
                                            </Grid.ColumnDefinitions>
                                            <ScrollBar x:Name="PART_HorizontalScrollBar" Grid.Column="1" Maximum="{TemplateBinding ScrollableWidth}" Orientation="Horizontal" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportWidth}"/>
                                        </Grid>
                                    </Grid>
                                </ControlTemplate>
                            </ScrollViewer.Template>
                            <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </ScrollViewer>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

The important part that makes the change possible is halfway through that code snippet where I set Margin="-17,0,0,0" for ScrollBar.

My current problem is that I can't figure out which component to offset to the left by 17 (by adding either margin or padding). I've been messing with all of them and no luck thus far. I'll update as soon as I get it, unless someone figures it out before me. As it stands now, scrollbar will overlay anything in that last column until I fix the offset.

EDIT #2:

Please refer to the updated XAML code above. What I did was add Margin="0,0,17,0" to ScrollContentPresenter.

One side effect effect, which I can completely live with, is the fact that offsets the header as well, but you don't see that until you scroll all the way to the right. It effects only the header of the last column, because the ScrollContentPresenter offsets it as well... strangely, since there is a DataGridColumnHeadersPresenter, but it works independently... so, I'll keep on working on it. ScrollContentPresenter, unfortunately, doesn't have padding, which would work like a charm. So, now I have to figure out how I can pad it, rather than setting a margin, or figure out a different method.

A similar method is to set the margin of the horizontal ScrollBar (that's the one in the second grid) to 0,0,-17,0. It'll move it 17 units to the right. Then, set the margin of DataGridColumnHeaderPresenter to 0,0,-17,0. It'll move it 17 units to the right.

EDIT #3:

Here's another method for anyone that's interested:

Set the margin of vertical ScrollBar to 0,-22,0,-17. -22 is the height of my headers, so adjust it to whatever yours are. This method stretches the scrollbar to cover the two white boxes.

EDIT #4:

I figured out the solution. Please see my answer for the XAML code, a screenshot and an explanation.

解决方案

So, above I've played around with several methods, but none of them achieved that perfect look and fill that I wanted. So, here's the solution that I'm very satisfied with. I hope that it helps others. I used Blend to go through every line of the template code and this is the best combination of things for my purpose.

Final result:

Pardon the messy headers, I covered them up on purpose. As you can see, the vertical scroll bar lines up nicely between the headers and the horizontal scroll bar located at the bottom. This was accomplished through setting margin of vertical ScrollBar (located in the first grid of the template) to -17 on the left side. Margin="-17,0,0,0". I then set the margin of ScrollContentProperty, located right above the vertical scroll bar within the template code, to 17 on the right side, Margin="0,0,17,0, to ensure that the vertical scroll bar doesn't cover any content. This leaves us with the header having 17 units of empty space to the right when we scroll to the extreme right, and that's due to the fact that our 17 margin on ScrollContentPresenter effects the header as well. To counter that, I created a style for DataGridColumnHeader and set its margin to -17 on the right side, Margin="0,0,-17,0", ensuring that it bleeds over into the empty space. To ensure that the last column header text is not hovering over the vertical scroll bar, making it visually unappealing, I added a padding of 17 to the right side, Padding="0,0,17,0".

Here's my XAML code for the DataGrid and DataGridColumnHeader that made this possible:

DataGrid:

    <Style TargetType="{x:Type DataGrid}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGrid}">
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True">
                        <ScrollViewer x:Name="DG_ScrollViewer" Focusable="false">
                            <ScrollViewer.Template>
                                <ControlTemplate TargetType="{x:Type ScrollViewer}">
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="Auto"/>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="Auto"/>
                                        </Grid.ColumnDefinitions>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition Height="*"/>
                                            <RowDefinition Height="Auto"/>
                                        </Grid.RowDefinitions>
                                        <Button Command="{x:Static DataGrid.SelectAllCommand}" Focusable="false" Style="{DynamicResource {ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}}" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.All}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Width="{Binding CellsPanelHorizontalOffset, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
                                        <DataGridColumnHeadersPresenter x:Name="PART_ColumnHeadersPresenter" Grid.Column="1" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Column}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
                                        <!--This is the scroll content presenter that gets shifted to the left 17 units so that scrollbar doesn't cover it-->
                                        <ScrollContentPresenter Margin="0,0,17,0" x:Name="PART_ScrollContentPresenter" CanContentScroll="{TemplateBinding CanContentScroll}" Grid.ColumnSpan="2" Grid.Row="1"/>
                                        <!--This is the vertical scrollbar. Margin is used to shift it to the left 17 units over the content-->
                                        <ScrollBar Margin="-17,0,0,0" x:Name="PART_VerticalScrollBar" Grid.Column="2" Maximum="{TemplateBinding ScrollableHeight}" Orientation="Vertical" Grid.Row="1" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportHeight}"/>
                                        <Grid Grid.Column="1" Grid.Row="2">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="{Binding NonFrozenColumnsViewportHorizontalOffset, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
                                                <ColumnDefinition Width="*"/>
                                            </Grid.ColumnDefinitions>
                                            <ScrollBar x:Name="PART_HorizontalScrollBar" Grid.Column="1" Maximum="{TemplateBinding ScrollableWidth}" Orientation="Horizontal" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportWidth}"/>
                                        </Grid>
                                    </Grid>
                                </ControlTemplate>
                            </ScrollViewer.Template>
                            <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </ScrollViewer>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

DataGridColumnHeader:

    <Style TargetType="{x:Type DataGridColumnHeader}">
        <Setter Property="Height" Value="22"/>
        <!--Padding to shift header text of the last column to the left 17 units-->
        <Setter Property="Padding" Value="0,0,17,0"/>
        <!--Margin to shift the entire header to the right 17 units to fill the void-->
        <Setter Property="Margin" Value="0,0,-17,0"/>
    </Style>

A similar method would be to set margin of the horizontal scroll bar to -17, Margin="0,0,-17,0", set padding of DataGridColumnHeader to 17, Padding="0,0,17,0", and its margin to -17, Margin="0,0,-17,0". This achieves the same result.

这篇关于如何在WPF中修改DataGrid垂直滚动条的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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